Repository navigation
Conversation
|
Thanks for picking this up! 🙌 One case that still slips through: binding an array offset of the property by reference. class ArrayOffset
{
/** @var array<string, int> */
private array $counters = [];
public function bind(int &$counter): void
{
$this->counters['requests'] = &$counter;
}
public function reset(): void
{
$this->counters['requests'] = 0;
}
}On 9734c86 this still reports |
dca28a2 to
eb82740
Compare
…alized by ref with promoted properties
…alized by ref with static property
eb82740 to
845e12c
Compare
|
//cc @SanderMuller |
| /** | ||
| * @param array<ClassMethod> $methods methods gathered so far in the class | ||
| */ | ||
| public static function isReadThroughReference(Expr $referencedExpr, Scope $scope, array $methods): bool |
There was a problem hiding this comment.
I don't really like this AST based node-finder deep seek, but since it only happens for by-ref parameters/promoted-properties, I think it is fine
|
@janedbal could you have a look whether the failling phpstan check with is a bug in dead-code detector? |
| * | ||
| * Called from the native ClassStatementsGatherer in turbo-ext as well. | ||
| */ | ||
| final class PropertyReferenceBinding |
There was a problem hiding this comment.
Dunno if there is a better name/place for this service...
SanderMuller
left a comment
There was a problem hiding this comment.
I checked 845e12c against 2.3.x (6dc37db) on PHP 8.6, with and without Turbo. Both issue samples no longer report property.onlyWritten. I found one regression and two smaller points.
-
With
checkUninitializedProperties: true, the binding now reports an uninitialized access. The sample from phpstan/phpstan#15886 gives this on line 22, which is$this->forwarded = &$forwarded;:Access to an uninitialized property class@anonymous/issue15886.php:10::$forwarded.2.3.x does not report it. The new
PropertyReadcarries the scope from before the assignment.ClassPropertiesNode::getUninitializedProperties()then sees a read in the constructor before the property has a value. Across the PR's fixtures and my probes I get 11 of these new errors, with and without Turbo. A@readonlyPHPDoc property getsproperty.uninitializedReadonlyByPhpDoceven without the option:/** * @readonly * @var list<string> */ private array $cache; /** @param list<string> $c */ public function __construct(array &$c) { $this->cache = &$c; }
I tried a flag on
PropertyRead,isThroughReference(), set for the two new reads. The premature access check then skips the reads that have it. All 11 errors go away, theonlyWrittenresults stay the same, and the DeadCode and Properties rule tests pass. I did not port it to the C++ side, wherepushNodeScope()would need to pass the flag too. -
A by-value parameter that is read after the binding is still reported:
/** @param list<string> $tmp @return list<string> */ public function run(array $tmp): array { $this->cache = &$tmp; $this->add(); return $tmp; }
The parameter loop returns
$param->byRefat once, so this case never gets the usage scan that local variables get. If a by-value parameter continues to that scan, this case is fixed, andByValueParameterin the fixture is still reported. -
Each binding can walk the whole method body twice. I put 1000 bindings like
$t1 = [1]; $this->p[1] = &$t1;in one method. Analysis took 3.09 to 3.28 s on 2.3.x and 11.6 to 12.9 s with this PR, in 3 rounds. With the walks skipped, the PR took 2.5 to 4.7 s. At 250 bindings the difference was within the noise, and real code rarely has this many bindings in one method.One walk per method, with the result kept per variable name, would avoid the repeated walks. The new code only runs for a reference assignment to a property and for a promoted by-ref parameter, so other code is not affected.
The other cases I checked are fine:
- The new tests fail with the
srcchange reverted. - A by-ref parameter of a closure, a nested offset (
$this->c['a']['b'] = &$counter) and aforeachby-ref value are now not reported. They were false positives on 2.3.x. - A promoted by-value property that is never read is still reported.
- Turbo: the smoke test and the signature parity check pass. The DeadCode tests pass with the extension loaded, and the probe output is the same with and without it.
- The full test suite passes with and without Turbo.
make phpstanand phpcs on the changed files are clean.
Two cases are now silent although nothing reads the property. One uses the same variable name in an unrelated closure, the other reads the local before the binding. The scan does not look at scope or position. Those cases only hide a report, so I would leave them.
The PHPStan (8.6, windows-latest) red is the same out-of-memory error in dead-code-detector that #7257 and #7258 have. The Benchmarks shards also fail on the 2.3.x push.
when a property is assigned from a reference passed in from a caller via parameter, the caller might read the results which the current scope writes. so it should not be consdered a "never read" property.
closes phpstan/phpstan#15886
closes phpstan/phpstan#10068