Skip to content

Commit

Permalink
Declare more precise phpdoc types (#993)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Apr 19, 2024
1 parent 46be456 commit c5ee33d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: "Install dependencies" - name: "Install dependencies"
run: | run: |
composer require php-coveralls/php-coveralls:^2.2 --dev --no-update 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" - name: "Tests"
run: "php vendor/bin/phpunit --coverage-clover build/logs/clover.xml" run: "php vendor/bin/phpunit --coverage-clover build/logs/clover.xml"
- name: Coveralls - name: Coveralls
Expand Down Expand Up @@ -49,7 +49,7 @@ jobs:
ini-file: "development" ini-file: "development"
tools: composer:v2 tools: composer:v2
- name: "Install dependencies" - 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" - name: "PHPUnit"
run: "php vendor/bin/phpunit" run: "php vendor/bin/phpunit"
test_old_73_80: test_old_73_80:
Expand All @@ -66,7 +66,7 @@ jobs:
ini-file: "development" ini-file: "development"
tools: composer:v2 tools: composer:v2
- name: "Install PHP 8 dependencies" - 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" - name: "Tests"
run: "test_old/run-php-src.sh 7.4.33" run: "test_old/run-php-src.sh 7.4.33"
test_old_80_70: test_old_80_70:
Expand All @@ -83,7 +83,7 @@ jobs:
ini-file: "development" ini-file: "development"
tools: composer:v2 tools: composer:v2
- name: "Install PHP 8 dependencies" - 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" - name: "Tests"
run: "test_old/run-php-src.sh 8.3.0RC2" run: "test_old/run-php-src.sh 8.3.0RC2"
phpstan: phpstan:
Expand Down
1 change: 1 addition & 0 deletions lib/PhpParser/Node.php
Expand Up @@ -6,6 +6,7 @@ interface Node {
/** /**
* Gets the type of the node. * Gets the type of the node.
* *
* @psalm-return non-empty-string
* @return string Type of the node * @return string Type of the node
*/ */
public function getType(): string; public function getType(): string;
Expand Down
12 changes: 11 additions & 1 deletion lib/PhpParser/Node/Identifier.php
Expand Up @@ -8,7 +8,10 @@
* Represents a non-namespaced name. Namespaced names are represented using Name nodes. * Represents a non-namespaced name. Namespaced names are represented using Name nodes.
*/ */
class Identifier extends NodeAbstract { class Identifier extends NodeAbstract {
/** @var string Identifier as string */ /**
* @psalm-var non-empty-string
* @var string Identifier as string
*/
public string $name; public string $name;


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

$this->attributes = $attributes; $this->attributes = $attributes;
$this->name = $name; $this->name = $name;
} }
Expand All @@ -36,6 +43,7 @@ public function getSubNodeNames(): array {
/** /**
* Get identifier as string. * Get identifier as string.
* *
* @psalm-return non-empty-string
* @return string Identifier as string. * @return string Identifier as string.
*/ */
public function toString(): string { public function toString(): string {
Expand All @@ -45,6 +53,7 @@ public function toString(): string {
/** /**
* Get lowercased identifier as string. * Get lowercased identifier as string.
* *
* @psalm-return non-empty-string
* @return string Lowercased identifier as string * @return string Lowercased identifier as string
*/ */
public function toLowerString(): string { public function toLowerString(): string {
Expand All @@ -63,6 +72,7 @@ public function isSpecialClassName(): bool {
/** /**
* Get identifier as string. * Get identifier as string.
* *
* @psalm-return non-empty-string
* @return string Identifier as string * @return string Identifier as string
*/ */
public function __toString(): string { public function __toString(): string {
Expand Down
11 changes: 10 additions & 1 deletion lib/PhpParser/Node/Name.php
Expand Up @@ -5,7 +5,10 @@
use PhpParser\NodeAbstract; use PhpParser\NodeAbstract;


class Name extends NodeAbstract { class Name extends NodeAbstract {
/** @var string Name as string */ /**
* @psalm-var non-empty-string
* @var string Name as string
*/
public string $name; public string $name;


/** @var array<string, bool> */ /** @var array<string, bool> */
Expand Down Expand Up @@ -33,6 +36,7 @@ public function getSubNodeNames(): array {
/** /**
* Get parts of name (split by the namespace separator). * Get parts of name (split by the namespace separator).
* *
* @psalm-return non-empty-list<string>
* @return string[] Parts of name * @return string[] Parts of name
*/ */
public function getParts(): array { public function getParts(): array {
Expand Down Expand Up @@ -103,6 +107,7 @@ public function isRelative(): bool {
* Returns a string representation of the name itself, without taking the name type into * 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). * account (e.g., not including a leading backslash for fully qualified names).
* *
* @psalm-return non-empty-string
* @return string String representation * @return string String representation
*/ */
public function toString(): string { public function toString(): string {
Expand All @@ -113,6 +118,7 @@ public function toString(): string {
* Returns a string representation of the name as it would occur in code (e.g., including * Returns a string representation of the name as it would occur in code (e.g., including
* leading backslash for fully qualified names. * leading backslash for fully qualified names.
* *
* @psalm-return non-empty-string
* @return string String representation * @return string String representation
*/ */
public function toCodeString(): string { public function toCodeString(): string {
Expand All @@ -123,6 +129,7 @@ public function toCodeString(): string {
* Returns lowercased string representation of the name, without taking the name type into * Returns lowercased string representation of the name, without taking the name type into
* account (e.g., no leading backslash for fully qualified names). * account (e.g., no leading backslash for fully qualified names).
* *
* @psalm-return non-empty-string
* @return string Lowercased string representation * @return string Lowercased string representation
*/ */
public function toLowerString(): string { public function toLowerString(): string {
Expand All @@ -142,6 +149,7 @@ public function isSpecialClassName(): bool {
* Returns a string representation of the name by imploding the namespace parts with the * Returns a string representation of the name by imploding the namespace parts with the
* namespace separator. * namespace separator.
* *
* @psalm-return non-empty-string
* @return string String representation * @return string String representation
*/ */
public function __toString(): string { public function __toString(): string {
Expand Down Expand Up @@ -237,6 +245,7 @@ public static function concat($name1, $name2, array $attributes = []) {
* *
* @param string|string[]|self $name Name to prepare * @param string|string[]|self $name Name to prepare
* *
* @psalm-return non-empty-string
* @return string Prepared name * @return string Prepared name
*/ */
private static function prepareName($name): string { private static function prepareName($name): string {
Expand Down
5 changes: 5 additions & 0 deletions test/PhpParser/Node/IdentifierTest.php
Expand Up @@ -3,6 +3,11 @@
namespace PhpParser\Node; namespace PhpParser\Node;


class IdentifierTest extends \PHPUnit\Framework\TestCase { class IdentifierTest extends \PHPUnit\Framework\TestCase {
public function testConstructorThrows() {
self::expectException(\InvalidArgumentException::class);
new Identifier('');
}

public function testToString() { public function testToString() {
$identifier = new Identifier('Foo'); $identifier = new Identifier('Foo');


Expand Down

0 comments on commit c5ee33d

Please sign in to comment.