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

Declare more precise phpdoc types #993

Merged
merged 8 commits into from
Apr 19, 2024
Merged

Declare more precise phpdoc types #993

merged 8 commits into from
Apr 19, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Apr 12, 2024

goal: ease working in projects which are static analysis covered.

we are doing the same thing in PHPStan with phpstan/phpstan-src#3013 (review)

@@ -8,6 +8,7 @@ class Name extends NodeAbstract
{
/**
* @var string[] Parts of the name
* @psalm-var non-empty-array<string>
* @deprecated Use getParts() instead
*/
public $parts;
Copy link
Contributor Author

@staabm staabm Apr 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could even declare * @param non-empty-string|non-empty-array<string>|self $name in __construct but since more strict param types could be considered a BC break, I did not add this change.

@staabm staabm marked this pull request as ready for review April 12, 2024 20:41
@staabm
Copy link
Contributor Author

staabm commented Apr 12, 2024

in the same turn I think about making the $type in Identifier a non-empty-string.. do you think this would work?

@nikic
Copy link
Owner

nikic commented Apr 13, 2024

Can you please target this at master instead?

in the same turn I think about making the $type in Identifier a non-empty-string.. do you think this would work?

I think that's fine. Empty string identifiers need to be written as something like {''}, in which case they're not Identifiers anymore.

@staabm staabm changed the base branch from 4.x to master April 13, 2024 06:25
@staabm
Copy link
Contributor Author

staabm commented Apr 13, 2024

the CI build on php-parser@v5 looks pretty broken. it seems there is some chicken-egg problem regarding deps and v4.
I don't think its related to this PR

@staabm
Copy link
Contributor Author

staabm commented Apr 13, 2024

after a deep dive into the CI problem I found a solution (its still not caused by this PR though).
My guess is, that this errors only occur in pull requests from forks.

the last remaining question is #993 (comment)

Thank you.

@ondrejmirtes
Copy link
Contributor

I’m not a huge fan of this. More precise parameter types will break static analysis in many projects, forcing people to write more precise PHPDoc types.

These types of changes should be done only in a major version IMHO.

@staabm
Copy link
Contributor Author

staabm commented Apr 13, 2024

I’m not a huge fan of this. More precise parameter types will break static analysis in many projects, forcing people to write more precise PHPDoc types.

Yeah thats why I asked. I am fine with return type improvements only

@ondrejmirtes
Copy link
Contributor

Yeah, return types should be fine, although it's a slight BC break too.

Imagine you're extending a class and typehinting string return type.

If the parent narrows the return type, suddenly the child return type is no longer covariant.

@@ -20,7 +20,7 @@ jobs:
- name: "Install dependencies"
run: |
composer require php-coveralls/php-coveralls:^2.2 --dev --no-update
composer update --no-progress --prefer-dist
COMPOSER_ROOT_VERSION=dev-master composer update --no-progress --prefer-dist
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do / why is it necessary?

Copy link
Contributor Author

@staabm staabm Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without this fix, CI errors in PRs of forks. see #993 (comment)

errors beeing fixed can be seen here: https://github.com/nikic/PHP-Parser/actions/runs/8671744331/job/23781260607?pr=993

Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - phpunit/phpunit[9.6.12, ..., 9.6.19] require phpunit/php-code-coverage ^9.2.28 -> satisfiable by phpunit/php-code-coverage[9.2.28, 9.2.29, 9.2.30, 9.2.31].
    - phpunit/phpunit[9.5.16, ..., 9.6.11] require phpunit/php-code-coverage ^9.2.13 -> satisfiable by phpunit/php-code-coverage[9.2.13, ..., 9.2.31].
    - phpunit/phpunit[9.5.10, ..., 9.5.14] require phpunit/php-code-coverage ^9.2.7 -> satisfiable by phpunit/php-code-coverage[9.2.7, ..., 9.2.31].
    - phpunit/phpunit[9.5.0, ..., 9.5.9] require phpunit/php-code-coverage ^9.2.3 -> satisfiable by phpunit/php-code-coverage[9.2.3, ..., 9.2.31].
    - phpunit/phpunit[9.4.0, ..., 9.4.4] require phpunit/php-code-coverage ^9.2 -> satisfiable by phpunit/php-code-coverage[9.2.0, ..., 9.2.31].
    - phpunit/phpunit 9.3.11 requires phpunit/php-code-coverage ^9.1.11 -> satisfiable by phpunit/php-code-coverage[9.1.11, ..., 9.2.31].
    - phpunit/phpunit[9.3.8, ..., 9.3.10] require phpunit/php-code-coverage ^9.1.5 -> satisfiable by phpunit/php-code-coverage[9.1.5, ..., 9.2.31].
    - phpunit/phpunit[9.3.4, ..., 9.3.7] require phpunit/php-code-coverage ^9.1.1 -> satisfiable by phpunit/php-code-coverage[9.1.1, ..., 9.2.31].
    - phpunit/phpunit[9.3.0, ..., 9.3.3] require phpunit/php-code-coverage ^9.0 -> satisfiable by phpunit/php-code-coverage[9.0.0, ..., 9.2.31].
    - phpunit/phpunit[9.0.0, ..., 9.2.6] require php ^7.3 -> your php version (8.3.6) does not satisfy that requirement.
    - phpunit/php-code-coverage[9.0.0, ..., 9.1.2] require nikic/php-parser ^4.7 -> satisfiable by nikic/php-parser[v4.7.0, ..., 4.x-dev] from composer repo (https://repo.packagist.org/) but nikic/php-parser is the root package and cannot be modified. See https://getcomposer.org/dep-on-root for details and assistance.
    - phpunit/php-code-coverage[9.1.3, ..., 9.2.2] require nikic/php-parser ^4.8 -> satisfiable by nikic/php-parser[v4.8.0, ..., 4.x-dev] from composer repo (https://repo.packagist.org/) but nikic/php-parser is the root package and cannot be modified. See https://getcomposer.org/dep-on-root for details and assistance.
    - phpunit/php-code-coverage[9.2.3, ..., 9.2.6] require nikic/php-parser ^4.10.2 -> satisfiable by nikic/php-parser[v4.10.2, ..., 4.x-dev] from composer repo (https://repo.packagist.org/) but nikic/php-parser is the root package and cannot be modified. See https://getcomposer.org/dep-on-root for details and assistance.
    - phpunit/php-code-coverage 9.2.7 requires nikic/php-parser ^4.12.0 -> satisfiable by nikic/php-parser[v4.12.0, ..., 4.x-dev] from composer repo (https://repo.packagist.org/) but nikic/php-parser is the root package and cannot be modified. See https://getcomposer.org/dep-on-root for details and assistance.
    - phpunit/php-code-coverage[9.2.8, ..., 9.2.15] require nikic/php-parser ^4.13.0 -> satisfiable by nikic/php-parser[v4.13.0, ..., 4.x-dev] from composer repo (https://repo.packagist.org/) but nikic/php-parser is the root package and cannot be modified. See https://getcomposer.org/dep-on-root for details and assistance.
    - phpunit/php-code-coverage[9.2.16, ..., 9.2.24] require nikic/php-parser ^4.14 -> satisfiable by nikic/php-parser[v4.14.0, ..., 4.x-dev] from composer repo (https://repo.packagist.org/) but nikic/php-parser is the root package and cannot be modified. See https://getcomposer.org/dep-on-root for details and assistance.
    - phpunit/php-code-coverage[9.2.25, ..., 9.2.29] require nikic/php-parser ^4.15 -> satisfiable by nikic/php-parser[v4.15.0, ..., 4.x-dev] from composer repo (https://repo.packagist.org/) but nikic/php-parser is the root package and cannot be modified. See https://getcomposer.org/dep-on-root for details and assistance.
    - phpunit/php-code-coverage[9.2.30, ..., 9.2.31] require nikic/php-parser ^4.18 || ^5.0 -> satisfiable by nikic/php-parser[dev-master, v4.18.0, v4.19.0, v4.19.1, 4.x-dev, v5.0.0alpha1, ..., 5.0.x-dev (alias of dev-master)] from composer repo (https://repo.packagist.org/) but nikic/php-parser is the root package and cannot be modified. See https://getcomposer.org/dep-on-root for details and assistance.
    - Root composer.json requires phpunit/phpunit ^9.0 -> satisfiable by phpunit/phpunit[9.0.0, ..., 9.6.19].

Error: Your requirements could not be resolved to an installable set of packages.

without thix fix composer does not realize what the version of the package of the checked-out project is

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I guess I caused this with b43758e and previously this probably just resolved to PHPUnit 8 instead...

@@ -25,6 +28,10 @@ class Identifier extends NodeAbstract {
* @param array<string, mixed> $attributes Additional attributes
*/
public function __construct(string $name, array $attributes = []) {
if ($name === '') {
throw new \InvalidArgumentException('Identifier name cannot be empty');
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikic nikic merged commit c5ee33d into nikic:master Apr 19, 2024
9 checks passed
@staabm staabm deleted the types branch April 19, 2024 12:20
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