Skip to content

Commit 16a1d52

Browse files
committed
Build/Test Tools: Fix static analysis issues in HashNotationVisitor.
This fixes the level-10 PHPStan errors that predate the hash notation visitor's recent changes, along with a few PhpStorm inspection warnings, without changing the `@phpstan-*` tags the visitor derives across `src/`. Most of the errors came from one limit: an entry in a hash can open a hash of its own, and an array shape cannot refer to itself, so every nested entry was `mixed`. Entries are therefore read into a new `HashEntry` class, whose children are typed as entries in turn. The rest came from writes through a list offset, which PHPStan reads as adding an incomplete element, so a tag or entry is now completed before it is added to its list. String types are also narrowed where possible, though an entry's type and key are only non-empty strings, since `0` is valid as either. Developed in WordPress#15384. Follow-up to r63420, r63789, r65217. Props westonruter, lancewillett. See #65817. git-svn-id: https://develop.svn.wordpress.org/trunk@65246 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e98fa1c commit 16a1d52

3 files changed

Lines changed: 145 additions & 61 deletions

File tree

‎tests/phpstan/HashEntry.php‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* One `@type` entry of a hash, as read by `HashNotationVisitor`.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace WordPress\PHPStan;
11+
12+
/**
13+
* One `@type` entry of a hash, with the hash it opens, if any.
14+
*
15+
* This is a class rather than an array shape because an entry may open a hash of
16+
* its own, and an array shape cannot refer to itself, while a class can: its
17+
* `$children` are entries in turn.
18+
*/
19+
final class HashEntry {
20+
21+
/**
22+
* Type as written in the docblock.
23+
*
24+
* @var non-empty-string
25+
*/
26+
public string $type;
27+
28+
/**
29+
* Key name, without the `$`.
30+
*
31+
* Not a non-falsy string, since the `$0` of a `...$0` entry is a key of `0`.
32+
*
33+
* @var non-empty-string
34+
*/
35+
public string $name;
36+
37+
/**
38+
* Whether the entry is written as `...$name`, describing a repeated value.
39+
*
40+
* @var bool
41+
*/
42+
public bool $variadic;
43+
44+
/**
45+
* Description of the entry.
46+
*
47+
* Once `HashNotationVisitor::parse_entries()` has read the hash an entry opens,
48+
* any text before the entry's `{` and the intro of that hash are on lines of
49+
* their own, since the markers are matched per line.
50+
*
51+
* @var string
52+
*/
53+
public string $description;
54+
55+
/**
56+
* Entries of the hash this entry opens, or empty if it opens none.
57+
*
58+
* @var list<HashEntry>
59+
*/
60+
public array $children = array();
61+
62+
/**
63+
* Constructor.
64+
*
65+
* @param non-empty-string $type Type as written in the docblock.
66+
* @param non-empty-string $name Key name, without the `$`.
67+
* @param bool $variadic Whether the entry is written as `...$name`.
68+
* @param string $description Description of the entry.
69+
*/
70+
public function __construct( string $type, string $name, bool $variadic, string $description ) {
71+
$this->type = $type;
72+
$this->name = $name;
73+
$this->variadic = $variadic;
74+
$this->description = $description;
75+
}
76+
}

0 commit comments

Comments
 (0)