-
Notifications
You must be signed in to change notification settings - Fork 523
Handle condition with always true/false sub-condition #3313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
VincentLanglet
wants to merge
3
commits into
phpstan:1.11.x
from
VincentLanglet:improveTypeSpecificer
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,9 @@ protected function setUp(): void | |
$this->scope = $this->scope->assignVariable('barOrFalse', new UnionType([new ObjectType('Bar'), new ConstantBooleanType(false)]), new UnionType([new ObjectType('Bar'), new ConstantBooleanType(false)])); | ||
$this->scope = $this->scope->assignVariable('stringOrFalse', new UnionType([new StringType(), new ConstantBooleanType(false)]), new UnionType([new StringType(), new ConstantBooleanType(false)])); | ||
$this->scope = $this->scope->assignVariable('array', new ArrayType(new MixedType(), new MixedType()), new ArrayType(new MixedType(), new MixedType())); | ||
$this->scope = $this->scope->assignVariable('arrayOrNull', new UnionType([new ArrayType(new MixedType(), new MixedType()), new NullType()]), new UnionType([new ArrayType(new MixedType(), new MixedType()), new NullType()])); | ||
$this->scope = $this->scope->assignVariable('foo', new MixedType(), new MixedType()); | ||
$this->scope = $this->scope->assignVariable('mixed', new MixedType(), new MixedType()); | ||
$this->scope = $this->scope->assignVariable('classString', new ClassStringType(), new ClassStringType()); | ||
$this->scope = $this->scope->assignVariable('genericClassString', new GenericClassStringType(new ObjectType('Bar')), new GenericClassStringType(new ObjectType('Bar'))); | ||
$this->scope = $this->scope->assignVariable('object', new ObjectWithoutClassType(), new ObjectWithoutClassType()); | ||
|
@@ -349,9 +351,17 @@ public function dataCondition(): iterable | |
$this->createFunctionCall('is_int', 'foo'), | ||
$this->createFunctionCall('is_string', 'bar'), | ||
), | ||
[], | ||
['$foo' => 'int'], | ||
['$foo' => '~int', '$bar' => '~string'], | ||
], | ||
[ | ||
new Expr\BinaryOp\BooleanOr( | ||
$this->createFunctionCall('is_int', 'foo'), | ||
$this->createFunctionCall('is_string', 'mixed'), | ||
), | ||
[], | ||
['$foo' => '~int', '$mixed' => '~string'], | ||
], | ||
[ | ||
new Expr\BinaryOp\BooleanAnd( | ||
new Expr\BinaryOp\BooleanOr( | ||
|
@@ -648,19 +658,37 @@ public function dataCondition(): iterable | |
[ | ||
new Expr\Empty_(new Variable('array')), | ||
[ | ||
'$array' => 'array{}|null', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since
we can be sure it's an array. |
||
'$array' => 'array{}', | ||
], | ||
[ | ||
'$array' => '~0|0.0|\'\'|\'0\'|array{}|false|null', | ||
], | ||
], | ||
[ | ||
new Expr\Empty_(new Variable('arrayOrNull')), | ||
[ | ||
'$arrayOrNull' => 'array{}|null', | ||
], | ||
[ | ||
'$arrayOrNull' => '~0|0.0|\'\'|\'0\'|array{}|false|null', | ||
], | ||
], | ||
[ | ||
new BooleanNot(new Expr\Empty_(new Variable('array'))), | ||
[ | ||
'$array' => '~0|0.0|\'\'|\'0\'|array{}|false|null', | ||
], | ||
[ | ||
'$array' => 'array{}|null', | ||
'$array' => 'array{}', | ||
], | ||
], | ||
[ | ||
new BooleanNot(new Expr\Empty_(new Variable('arrayOrNull'))), | ||
[ | ||
'$arrayOrNull' => '~0|0.0|\'\'|\'0\'|array{}|false|null', | ||
], | ||
[ | ||
'$arrayOrNull' => 'array{}|null', | ||
], | ||
], | ||
[ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug11276; | ||
|
||
class HelloWorld | ||
{ | ||
/** | ||
* @param array{from: string, to: string} $expected | ||
*/ | ||
public function testLanguagesMatchingRegex(string $url, ?array $expected): void | ||
{ | ||
preg_match('#\/(?<from>[a-z]{2})-(?<to>[a-z]{1}[a-z0-9]{1})\/#', $url, $matches); | ||
|
||
foreach ($expected as $key => $value) { | ||
if ($matches instanceof ArrayAccess || \array_key_exists($key, $matches)) { | ||
$matches[$key]; | ||
} | ||
} | ||
|
||
foreach ($expected as $key => $value) { | ||
if (\array_key_exists($key, $matches) || $matches instanceof ArrayAccess) { | ||
$matches[$key]; | ||
} | ||
} | ||
|
||
foreach ($expected as $key => $value) { | ||
if (!$matches instanceof ArrayAccess && !\array_key_exists($key, $matches)) { | ||
} else { | ||
$matches[$key]; | ||
} | ||
} | ||
|
||
foreach ($expected as $key => $value) { | ||
if (!\array_key_exists($key, $matches) && !$matches instanceof ArrayAccess) { | ||
} else { | ||
$matches[$key]; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
bar
is an objectwe can be sure foo is an int.
I added the same test with
is_int($foo) || is_string($mixed)
to reproduce the old behavior.