Skip to content

Conversation

@staabm
Copy link
Contributor

@staabm staabm commented May 12, 2022

try to workout the remaining problems of #1301

@staabm staabm marked this pull request as ready for review May 12, 2022 08:37
@staabm
Copy link
Contributor Author

staabm commented May 12, 2022

I think this is a good next step into finishing it. the remaining error seems unrelated.

next step (which I would do in a separate PR) is working on this part:

For one, I don't think that some extension types are called with the reordered arguments. Let's take dynamic method return type extensions for example

@staabm
Copy link
Contributor Author

staabm commented May 12, 2022

the remaining error seems unrelated.

it seems the remainig error is triggered because there is a dependency between

dynamic-method-throw-type-extension.php and dynamic-method-throw-type-extension-named-args.php which makes the former load classes which contain named parameters on php 7.3/7.4:

1) Error
The data provider specified for PHPStan\Analyser\DynamicMethodThrowTypeExtensionTest::testFileAsserts is invalid.
ParseError: syntax error, unexpected ':', expecting ')'
/home/runner/work/phpstan-src/phpstan-src/tests/PHPStan/Analyser/data/dynamic-method-throw-type-extension-named-args.php:90
/home/runner/work/phpstan-src/phpstan-src/vendor/nette/di/src/DI/Resolver.php:139
/home/runner/work/phpstan-src/phpstan-src/vendor/nette/di/src/DI/Definitions/ServiceDefinition.php:183
/home/runner/work/phpstan-src/phpstan-src/vendor/nette/di/src/DI/Resolver.php:71
/home/runner/work/phpstan-src/phpstan-src/vendor/nette/di/src/DI/ContainerBuilder.php:298
/home/runner/work/phpstan-src/phpstan-src/vendor/nette/di/src/DI/Compiler.php:262
/home/runner/work/phpstan-src/phpstan-src/vendor/nette/di/src/DI/Compiler.php:206
/home/runner/work/phpstan-src/phpstan-src/vendor/nette/di/src/DI/ContainerLoader.php:117
/home/runner/work/phpstan-src/phpstan-src/vendor/nette/di/src/DI/ContainerLoader.php:80
/home/runner/work/phpstan-src/phpstan-src/vendor/nette/di/src/DI/ContainerLoader.php:44
/home/runner/work/phpstan-src/phpstan-src/src/DependencyInjection/Configurator.php:46
/home/runner/work/phpstan-src/phpstan-src/vendor/nette/bootstrap/src/Bootstrap/Configurator.php:252
/home/runner/work/phpstan-src/phpstan-src/src/DependencyInjection/ContainerFactory.php:106
/home/runner/work/phpstan-src/phpstan-src/src/Testing/PHPStanTestCase.php:78
/home/runner/work/phpstan-src/phpstan-src/src/Testing/PHPStanTestCase.php:126
/home/runner/work/phpstan-src/phpstan-src/src/Testing/TypeInferenceTestCase.php:36
/home/runner/work/phpstan-src/phpstan-src/src/Testing/TypeInferenceTestCase.php:133
/home/runner/work/phpstan-src/phpstan-src/tests/PHPStan/Analyser/DynamicMethodThrowTypeExtensionTest.php:13

@ondrejmirtes
Copy link
Member

I've got a couple of ideas what should also be done here while I was thinking about this for the last 24 hours:

  1. We should stay as faithful to the simulated non-named arguments call as possible.

Which means that if we have function foo(int $one = 1, int $two = 2, int $three = 3), the following scenarios should be followed:

  • foo(2) should lead to args array with a single element - 2 - simulating foo(2)
  • foo(one: 2) should lead to args array with a single element - 2 - simulating foo(2).
  • foo(two: 2) should lead to args array with two elements: 1, 2. Simulating foo(1, 2).

This means that default values for optional parameters at the end shouldn't be provided if a named argument after them wasn't provided. That's why the last item isn't simulating foo(1, 2, 3), just foo(1, 2).

  1. The return types of the various NamedArgumentsHelper::reorder*Arguments methods should be nullable. Because we can't produce a valid args array (indexed from zero, no gaps in indexes) if someone omits a required parameter using named arguments. Let's take a similar signature (no optional parameters):

function foo(int $one, int $two, int $three)

If someone calls foo(two: 2), we have no value to place at index 0. In this case the NamedArgumentsHelper has to return null and no extensions will be called.

And similarly, if someone calls foo(1, 2) (named arguments were not used, but some required arguments are missing), we can also return null to prevent some nasty bugs in the extensions.

Even if the helper already behaves this way, please add new test cases verifying the correctness in a new commit so I can check them in one place :) Thank you.

@ondrejmirtes ondrejmirtes merged this pull request into phpstan:named-arguments May 12, 2022
@ondrejmirtes
Copy link
Member

For now I'm merging this into #1301, thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants