Repository: phpactor/phpactor (php)

Issue:
[CT] Complete constructor: Do not complete args which are passed to parent
i.e. do not do this:

```
  public function __construct(array $configuration, $plugin_id, $plugin_definition, BlockManagerInterface $block_manager)
  {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
        $this->configuration = $configuration;
        $this->plugin_id = $plugin_id;
        $this->plugin_definition = $plugin_definition;
        $this->block_manager = $block_manager;
  }
```

Pull request description:
Skip parent parameters on complete constructor
This solution was already implemented for #2333 I just copied it over to the `transformAssign` function which should also close #374 for good.

Patch:
diff --git a/lib/CodeTransform/Adapter/WorseReflection/Transformer/CompleteConstructor.php b/lib/CodeTransform/Adapter/WorseReflection/Transformer/CompleteConstructor.php
index afbda820..bf2b6699 100644
--- a/lib/CodeTransform/Adapter/WorseReflection/Transformer/CompleteConstructor.php
+++ b/lib/CodeTransform/Adapter/WorseReflection/Transformer/CompleteConstructor.php
@@ -92,14 +92,21 @@ class CompleteConstructor implements Transformer
             $constructMethod = $class->methods()->get('__construct');
             $methodBody = (string) $constructMethod->body();
 
-            foreach ($constructMethod->parameters()->notPromoted() as $parameter) {
+            // Filtering out parameters from the parent class
+            $nonPromotedParameterNames = $this->getParentClassParamaterNames($class);
+            $parametersToHandle = array_filter(
+                iterator_to_array($constructMethod->parameters()->notPromoted()),
+                fn (ReflectionParameter $parameter) => !in_array($parameter->name(), $nonPromotedParameterNames)
+            );
+
+            foreach ($parametersToHandle as $parameter) {
                 if (preg_match('{this\s*->' . $parameter->name() . '}', $methodBody)) {
                     continue;
                 }
                 $methodBuilder->body()->line('$this->' . $parameter->name() . ' = $' . $parameter->name() .';');
             }
 
-            foreach ($constructMethod->parameters()->notPromoted() as $parameter) {
+            foreach ($parametersToHandle as $parameter) {
                 if ($parameter->isPromoted()) {
                     continue;
                 }
@@ -109,7 +116,6 @@ class CompleteConstructor implements Transformer
                     continue;
                 }
 
-
                 $propertyBuilder = $classBuilder->property($parameter->name());
                 $propertyBuilder->visibility($this->visibility);
                 $parameterType = $parameter->inferredType();


Test patch:
diff --git a/lib/CodeTransform/Tests/Adapter/WorseReflection/Transformer/CompleteConstructorTest.php b/lib/CodeTransform/Tests/Adapter/WorseReflection/Transformer/CompleteConstructorTest.php
index 83fd6171..baa0d7ee 100644
--- a/lib/CodeTransform/Tests/Adapter/WorseReflection/Transformer/CompleteConstructorTest.php
+++ b/lib/CodeTransform/Tests/Adapter/WorseReflection/Transformer/CompleteConstructorTest.php
@@ -211,6 +211,39 @@ class CompleteConstructorTest extends WorseTestCase
 
         ];
 
+        yield 'it does not add parameters of inherited classes' => [
+            <<<'EOT'
+                    <?php
+                    class ParentClass
+                    {
+                        public function __construct(private string $someParam) {}
+                    }
+
+                    class Foobar extends ParentClass
+                    {
+                        public function __construct(string $someParam) {
+
+                            parent::__construct($someParam);
+                        }
+                    }
+                EOT,
+            <<<'EOT'
+                    <?php
+                    class ParentClass
+                    {
+                        public function __construct(private string $someParam) {}
+                    }
+
+                    class Foobar extends ParentClass
+                    {
+                        public function __construct(string $someParam) {
+
+                            parent::__construct($someParam);
+                        }
+                    }
+                EOT,
+        ];
+
         yield  'It adds type docblocks' => [
             <<<'EOT'
                 <?php