phpstan-src icon indicating copy to clipboard operation
phpstan-src copied to clipboard

declare assert-if-true for filesystem functions

Open staabm opened this issue 3 years ago • 3 comments

closes https://github.com/phpstan/phpstan/issues/6788

staabm avatar Nov 13 '22 19:11 staabm

good point.

2) PHPStan\Rules\Comparison\ImpossibleCheckTypeFunctionCallRuleTest::testBug6788
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'
+'13: Call to function chdir() with non-empty-string will always evaluate to true.
+    💡 Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.
+15: Call to function chdir() with non-falsy-string will always evaluate to true.
+    💡 Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.
 '

staabm avatar Nov 20 '22 12:11 staabm

the last commit is fixing cases like

<?php

namespace Bug4816;

use function PHPStan\Testing\assertType;

function (string $x): void {
	if (is_dir($x)) {
		assertType('true', is_dir($x));
	}
};

(so we don't regress https://github.com/phpstan/phpstan-src/commit/1eaef0463d2347bdbef3612da158853519867d82)


my understanding is, that because this PR adds stubs with @phpstan-assert the specifier does no longer invoke handleDefaultTruthyOrFalseyContext in

https://github.com/phpstan/phpstan-src/blob/7cd03f02a5c32ca4e51006dc21d6184f73835a14/src/Analyser/TypeSpecifier.php#L455-L472

and that the reason why my previous code sample did regress. After adding handleDefaultTruthyOrFalseyContext I can see other things regress though :).

@rvanvelzen could you give me a hand? why do we not invoke handleDefaultTruthyOrFalseyContext when taking the path thru specifyTypesFromAsserts. is this expected or an oversight?

btw: I noticed we also don't invoke handleDefaultTruthyOrFalseyContext when we take the path thru specifyTypesFromConditionalReturnType.

staabm avatar Jun 12 '24 12:06 staabm

I had no idea that the assertion would actually assert the opposite if the condition is not met. This is very counter-intuitive for me.. I think the behavior with "=" should be the default and there should rather be another modifier to request the double behavior.

thg2k avatar Jun 12 '24 14:06 thg2k