Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MethodGenerator parameter sorting, when an explicit sorting is provided #90

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,7 @@
<InvalidArgument occurrences="1">
<code>new stdClass()</code>
</InvalidArgument>
<MissingReturnType occurrences="20">
<MissingReturnType occurrences="22">
<code>testByRefReturnType</code>
<code>testCopyMethodSignature</code>
<code>testCreateFromArray</code>
Expand All @@ -1609,6 +1609,8 @@
<code>testMethodFromReflectionMultiLinesIndention</code>
<code>testMethodParameterAccessors</code>
<code>testMethodParameterMutator</code>
<code>testSetMethodParameter</code>
<code>testSetMethodParameters</code>
<code>testMethodWithFinalModifierIsEmitted</code>
<code>testMethodWithFinalModifierIsNotEmittedWhenMethodIsAbstract</code>
<code>testMethodWithStaticModifierIsEmitted</code>
Expand Down
15 changes: 15 additions & 0 deletions src/Generator/MethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function strtolower;
use function substr;
use function trim;
use function usort;

class MethodGenerator extends AbstractMemberGenerator
{
Expand Down Expand Up @@ -216,6 +217,8 @@ public function setParameters(array $parameters)
$this->setParameter($parameter);
}

$this->sortParameters();

return $this;
}

Expand Down Expand Up @@ -244,6 +247,8 @@ public function setParameter($parameter)

$this->parameters[$parameter->getName()] = $parameter;

$this->sortParameters();

return $this;
}

Expand Down Expand Up @@ -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
*/
Expand Down
35 changes: 35 additions & 0 deletions test/Generator/MethodGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use stdClass;

use function array_filter;
use function array_map;
use function array_shift;
use function array_values;

Expand Down Expand Up @@ -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);
willjones9 marked this conversation as resolved.
Show resolved Hide resolved
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();
Expand Down