diff --git a/psalm-baseline.xml b/psalm-baseline.xml
index 8ef09905..f3837836 100644
--- a/psalm-baseline.xml
+++ b/psalm-baseline.xml
@@ -1593,7 +1593,7 @@
new stdClass()
-
+
testByRefReturnType
testCopyMethodSignature
testCreateFromArray
@@ -1609,6 +1609,8 @@
testMethodFromReflectionMultiLinesIndention
testMethodParameterAccessors
testMethodParameterMutator
+ testSetMethodParameter
+ testSetMethodParameters
testMethodWithFinalModifierIsEmitted
testMethodWithFinalModifierIsNotEmittedWhenMethodIsAbstract
testMethodWithStaticModifierIsEmitted
diff --git a/src/Generator/MethodGenerator.php b/src/Generator/MethodGenerator.php
index 825d9109..e710cc77 100644
--- a/src/Generator/MethodGenerator.php
+++ b/src/Generator/MethodGenerator.php
@@ -15,6 +15,7 @@
use function strtolower;
use function substr;
use function trim;
+use function usort;
class MethodGenerator extends AbstractMemberGenerator
{
@@ -216,6 +217,8 @@ public function setParameters(array $parameters)
$this->setParameter($parameter);
}
+ $this->sortParameters();
+
return $this;
}
@@ -244,6 +247,8 @@ public function setParameter($parameter)
$this->parameters[$parameter->getName()] = $parameter;
+ $this->sortParameters();
+
return $this;
}
@@ -305,6 +310,16 @@ public function setReturnsReference($returnsReference)
return $this;
}
+ /**
+ * Sort parameters by their position
+ */
+ private function sortParameters(): void
+ {
+ usort($this->parameters, static function (ParameterGenerator $item1, ParameterGenerator $item2) {
+ return $item1->getPosition() <=> $item2->getPosition();
+ });
+ }
+
/**
* @return string
*/
diff --git a/test/Generator/MethodGeneratorTest.php b/test/Generator/MethodGeneratorTest.php
index 99dfb5c5..e6d1fd09 100644
--- a/test/Generator/MethodGeneratorTest.php
+++ b/test/Generator/MethodGeneratorTest.php
@@ -21,6 +21,7 @@
use stdClass;
use function array_filter;
+use function array_map;
use function array_shift;
use function array_values;
@@ -74,6 +75,40 @@ public function testMethodParameterMutator()
$methodGenerator->setParameter(new stdClass());
}
+ public function testSetMethodParameter()
+ {
+ $methodGenerator = new MethodGenerator();
+
+ $methodGenerator->setParameter('foo');
+
+ $params = $methodGenerator->getParameters();
+ self::assertCount(1, $params);
+
+ /** @var ParameterGenerator $foo */
+ $foo = array_shift($params);
+ self::assertInstanceOf(ParameterGenerator::class, $foo);
+ self::assertSame('foo', $foo->getName());
+ }
+
+ public function testSetMethodParameters()
+ {
+ $methodGenerator = new MethodGenerator();
+
+ $methodGenerator->setParameter('foo');
+ $methodGenerator->setParameter(['name' => 'bar', 'type' => 'array', 'position' => 2]);
+ $methodGenerator->setParameter(
+ ParameterGenerator::fromArray(['name' => 'baz', 'type' => stdClass::class, 'position' => 1])
+ );
+
+ $params = $methodGenerator->getParameters();
+
+ $sorting = array_map(static function (ParameterGenerator $parameter): string {
+ return $parameter->getName();
+ }, $params);
+
+ self::assertEquals(['foo', 'baz', 'bar'], $sorting);
+ }
+
public function testMethodBodyGetterAndSetter()
{
$method = new MethodGenerator();