From 999135f07074574b856a8fa59aa868990cee88cd Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 13 Apr 2024 08:32:55 +0200 Subject: [PATCH 1/8] Declare more precise phpdoc types --- lib/PhpParser/Node.php | 1 + lib/PhpParser/Node/Name.php | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/PhpParser/Node.php b/lib/PhpParser/Node.php index 843e2ea66c..fd2a9b7247 100644 --- a/lib/PhpParser/Node.php +++ b/lib/PhpParser/Node.php @@ -6,6 +6,7 @@ interface Node { /** * Gets the type of the node. * + * @psalm-return non-empty-string * @return string Type of the node */ public function getType(): string; diff --git a/lib/PhpParser/Node/Name.php b/lib/PhpParser/Node/Name.php index 26b863e40b..ef883ea0f1 100644 --- a/lib/PhpParser/Node/Name.php +++ b/lib/PhpParser/Node/Name.php @@ -5,7 +5,10 @@ use PhpParser\NodeAbstract; class Name extends NodeAbstract { - /** @var string Name as string */ + /** + * @psalm-var non-empty-string + * @var non-empty-string Name as string + */ public string $name; /** @var array */ @@ -33,6 +36,7 @@ public function getSubNodeNames(): array { /** * Get parts of name (split by the namespace separator). * + * @psalm-return non-empty-array * @return string[] Parts of name */ public function getParts(): array { @@ -103,6 +107,7 @@ public function isRelative(): bool { * Returns a string representation of the name itself, without taking the name type into * account (e.g., not including a leading backslash for fully qualified names). * + * @psalm-return non-empty-string * @return string String representation */ public function toString(): string { @@ -113,6 +118,7 @@ public function toString(): string { * Returns a string representation of the name as it would occur in code (e.g., including * leading backslash for fully qualified names. * + * @psalm-return non-empty-string * @return string String representation */ public function toCodeString(): string { @@ -123,6 +129,7 @@ public function toCodeString(): string { * Returns lowercased string representation of the name, without taking the name type into * account (e.g., no leading backslash for fully qualified names). * + * @psalm-return non-empty-string * @return string Lowercased string representation */ public function toLowerString(): string { @@ -142,6 +149,7 @@ public function isSpecialClassName(): bool { * Returns a string representation of the name by imploding the namespace parts with the * namespace separator. * + * @psalm-return non-empty-string * @return string String representation */ public function __toString(): string { @@ -237,7 +245,7 @@ public static function concat($name1, $name2, array $attributes = []) { * * @param string|string[]|self $name Name to prepare * - * @return string Prepared name + * @return non-empty-string Prepared name */ private static function prepareName($name): string { if (\is_string($name)) { From e244286926ec1f250f45ace4373dafb1f7695fbc Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 13 Apr 2024 08:37:35 +0200 Subject: [PATCH 2/8] non-empty-string in Identifier --- lib/PhpParser/Node/Identifier.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/PhpParser/Node/Identifier.php b/lib/PhpParser/Node/Identifier.php index 266166cbb6..6985626406 100644 --- a/lib/PhpParser/Node/Identifier.php +++ b/lib/PhpParser/Node/Identifier.php @@ -8,7 +8,10 @@ * Represents a non-namespaced name. Namespaced names are represented using Name nodes. */ class Identifier extends NodeAbstract { - /** @var string Identifier as string */ + /** + * @psalm-var non-empty-string + * @var string Identifier as string + */ public string $name; /** @var array */ @@ -21,6 +24,7 @@ class Identifier extends NodeAbstract { /** * Constructs an identifier node. * + * @psalm-param non-empty-string $name * @param string $name Identifier as string * @param array $attributes Additional attributes */ @@ -36,6 +40,7 @@ public function getSubNodeNames(): array { /** * Get identifier as string. * + * @psalm-return non-empty-string * @return string Identifier as string. */ public function toString(): string { @@ -45,6 +50,7 @@ public function toString(): string { /** * Get lowercased identifier as string. * + * @psalm-return non-empty-string * @return string Lowercased identifier as string */ public function toLowerString(): string { @@ -63,6 +69,7 @@ public function isSpecialClassName(): bool { /** * Get identifier as string. * + * @psalm-return non-empty-string * @return string Identifier as string */ public function __toString(): string { From 42a34bde26add381131ced0b0443b070d0727be1 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 13 Apr 2024 08:38:35 +0200 Subject: [PATCH 3/8] fix typo --- lib/PhpParser/Node/Name.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/PhpParser/Node/Name.php b/lib/PhpParser/Node/Name.php index ef883ea0f1..aa2b90eb22 100644 --- a/lib/PhpParser/Node/Name.php +++ b/lib/PhpParser/Node/Name.php @@ -7,7 +7,7 @@ class Name extends NodeAbstract { /** * @psalm-var non-empty-string - * @var non-empty-string Name as string + * @var string Name as string */ public string $name; @@ -36,7 +36,7 @@ public function getSubNodeNames(): array { /** * Get parts of name (split by the namespace separator). * - * @psalm-return non-empty-array + * @psalm-return non-empty-list * @return string[] Parts of name */ public function getParts(): array { @@ -245,7 +245,8 @@ public static function concat($name1, $name2, array $attributes = []) { * * @param string|string[]|self $name Name to prepare * - * @return non-empty-string Prepared name + * @psalm-return non-empty-string + * @return string Prepared name */ private static function prepareName($name): string { if (\is_string($name)) { From 911ba186e2dad524469962084ce8478f590192e9 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 13 Apr 2024 09:16:07 +0200 Subject: [PATCH 4/8] try fix CI --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index dba8dbc1a8..c54648fdae 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -49,7 +49,7 @@ jobs: ini-file: "development" tools: composer:v2 - name: "Install dependencies" - run: "composer update --no-progress --prefer-dist ${{ matrix.flags }}" + run: "COMPOSER_ROOT_VERSION=dev-master composer update --no-progress --prefer-dist ${{ matrix.flags }}" - name: "PHPUnit" run: "php vendor/bin/phpunit" test_old_73_80: From 318229701fce8cd4e131fd6c3117792cfff02021 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 13 Apr 2024 09:18:04 +0200 Subject: [PATCH 5/8] try fix more CI --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c54648fdae..39a077920d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -66,7 +66,7 @@ jobs: ini-file: "development" tools: composer:v2 - name: "Install PHP 8 dependencies" - run: "composer update --no-progress --prefer-dist" + run: "COMPOSER_ROOT_VERSION=dev-master composer update --no-progress --prefer-dist" - name: "Tests" run: "test_old/run-php-src.sh 7.4.33" test_old_80_70: @@ -83,7 +83,7 @@ jobs: ini-file: "development" tools: composer:v2 - name: "Install PHP 8 dependencies" - run: "composer update --no-progress --prefer-dist" + run: "COMPOSER_ROOT_VERSION=dev-master composer update --no-progress --prefer-dist" - name: "Tests" run: "test_old/run-php-src.sh 8.3.0RC2" phpstan: From e408e0b2e0b15ab90c034ebfc54121a445e76eca Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 13 Apr 2024 09:23:36 +0200 Subject: [PATCH 6/8] try fix coverage job --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 39a077920d..bf79544f54 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 - name: "Tests" run: "php vendor/bin/phpunit --coverage-clover build/logs/clover.xml" - name: Coveralls From ca150d9d4b4ae0d686919953320b931dfb18e3ea Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 13 Apr 2024 10:58:06 +0200 Subject: [PATCH 7/8] Revert stricter param type which would be a BC break. --- lib/PhpParser/Node/Identifier.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/PhpParser/Node/Identifier.php b/lib/PhpParser/Node/Identifier.php index 6985626406..01eebe5ca4 100644 --- a/lib/PhpParser/Node/Identifier.php +++ b/lib/PhpParser/Node/Identifier.php @@ -24,11 +24,14 @@ class Identifier extends NodeAbstract { /** * Constructs an identifier node. * - * @psalm-param non-empty-string $name * @param string $name Identifier as string * @param array $attributes Additional attributes */ public function __construct(string $name, array $attributes = []) { + if ($name === '') { + throw new \InvalidArgumentException('Identifier name cannot be empty'); + } + $this->attributes = $attributes; $this->name = $name; } From f917163a9bf17c90c8ead82d8c8293eedf38d618 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 19 Apr 2024 13:51:21 +0200 Subject: [PATCH 8/8] added test --- test/PhpParser/Node/IdentifierTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/PhpParser/Node/IdentifierTest.php b/test/PhpParser/Node/IdentifierTest.php index 74e7de54ac..3b52806b9c 100644 --- a/test/PhpParser/Node/IdentifierTest.php +++ b/test/PhpParser/Node/IdentifierTest.php @@ -3,6 +3,11 @@ namespace PhpParser\Node; class IdentifierTest extends \PHPUnit\Framework\TestCase { + public function testConstructorThrows() { + self::expectException(\InvalidArgumentException::class); + new Identifier(''); + } + public function testToString() { $identifier = new Identifier('Foo');