-
Notifications
You must be signed in to change notification settings - Fork 506
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
Implement TypeSpecifierContext->getReturnType() #3878
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6918438
Implement TypeSpecifierContext->getReturnType()
staabm 5be8079
Update TypeSpecifierContext.php
staabm 6999621
Update TypeSpecifierContext.php
staabm 68817de
Update TypeSpecifier.php
staabm e4cad70
Update TypeSpecifier.php
staabm f38718a
Update TypeSpecifier.php
staabm 431dca8
Update TypeSpecifier.php
staabm fca50f7
Update TypeSpecifierContext.php
staabm 58dc99a
Update TypeSpecifier.php
staabm 0b4b6cb
fix
staabm 877cbf6
Update type-specifier-context-return-type.php
staabm 2587cc7
Update TypeSpecifier.php
staabm 2c615a7
pass return type arround when re-creating context
staabm 3447717
Update TypeSpecifierContextReturnTypeExtension.php
staabm e1f4910
negate type in TypeSpecifierContext
staabm f4984d2
Update TypeSpecifier.php
staabm 35a7641
Update TypeSpecifierContext.php
staabm 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 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 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 |
---|---|---|
|
@@ -3,6 +3,9 @@ | |
namespace PHPStan\Analyser; | ||
|
||
use PHPStan\ShouldNotHappenException; | ||
use PHPStan\Type\GeneralizePrecision; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
|
||
/** | ||
* @api | ||
|
@@ -21,34 +24,42 @@ final class TypeSpecifierContext | |
/** @var self[] */ | ||
private static array $registry; | ||
|
||
private function __construct(private ?int $value) | ||
private function __construct( | ||
private ?int $value, | ||
private ?Type $returnType, | ||
) | ||
{ | ||
} | ||
|
||
private static function create(?int $value): self | ||
private static function create(?int $value, ?Type $returnType = null): self | ||
{ | ||
self::$registry[$value] ??= new self($value); | ||
if ($returnType !== null) { | ||
// return type bound context is unique for each context and therefore not cacheable | ||
return new self($value, $returnType); | ||
} | ||
|
||
self::$registry[$value] ??= new self($value, null); | ||
return self::$registry[$value]; | ||
} | ||
|
||
public static function createTrue(): self | ||
public static function createTrue(?Type $returnType = null): self | ||
{ | ||
return self::create(self::CONTEXT_TRUE); | ||
return self::create(self::CONTEXT_TRUE, $returnType); | ||
} | ||
|
||
public static function createTruthy(): self | ||
public static function createTruthy(?Type $returnType = null): self | ||
{ | ||
return self::create(self::CONTEXT_TRUTHY); | ||
return self::create(self::CONTEXT_TRUTHY, $returnType); | ||
} | ||
|
||
public static function createFalse(): self | ||
public static function createFalse(?Type $returnType = null): self | ||
{ | ||
return self::create(self::CONTEXT_FALSE); | ||
return self::create(self::CONTEXT_FALSE, $returnType); | ||
} | ||
|
||
public static function createFalsey(): self | ||
public static function createFalsey(?Type $returnType = null): self | ||
{ | ||
return self::create(self::CONTEXT_FALSEY); | ||
return self::create(self::CONTEXT_FALSEY, $returnType); | ||
} | ||
|
||
public static function createNull(): self | ||
|
@@ -61,7 +72,14 @@ public function negate(): self | |
if ($this->value === null) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
return self::create(~$this->value & self::CONTEXT_BITMASK); | ||
|
||
$negatedReturnType = null; | ||
if ($this->returnType !== null) { | ||
$baseType = $this->returnType->generalize(GeneralizePrecision::lessSpecific()); | ||
$negatedReturnType = TypeCombinator::remove($baseType, $this->returnType); | ||
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. This won't work for unions of int. We will need 2 two return types in the context and toggle between them on negate() |
||
} | ||
|
||
return self::create(~$this->value & self::CONTEXT_BITMASK, $negatedReturnType); | ||
} | ||
|
||
public function true(): bool | ||
|
@@ -89,4 +107,9 @@ public function null(): bool | |
return $this->value === null; | ||
} | ||
|
||
public function getReturnType(): ?Type | ||
{ | ||
return $this->returnType; | ||
} | ||
|
||
} |
This file contains 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
5 changes: 5 additions & 0 deletions
5
tests/PHPStan/Analyser/TypeSpecifierContextReturnTypeExtension.neon
This file contains 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,5 @@ | ||
services: | ||
- | ||
class: PHPStan\Tests\TypeSpecifierContextReturnTypeExtension | ||
tags: | ||
- phpstan.typeSpecifier.methodTypeSpecifyingExtension |
35 changes: 35 additions & 0 deletions
35
tests/PHPStan/Analyser/TypeSpecifierContextReturnTypeTest.php
This file contains 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,35 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Analyser; | ||
|
||
use PHPStan\Testing\TypeInferenceTestCase; | ||
|
||
class TypeSpecifierContextReturnTypeTest extends TypeInferenceTestCase | ||
{ | ||
|
||
public function dataContextReturnType(): iterable | ||
{ | ||
yield from $this->gatherAssertTypes(__DIR__ . '/data/type-specifier-context-return-type.php'); | ||
} | ||
|
||
/** | ||
* @dataProvider dataContextReturnType | ||
* @param mixed ...$args | ||
*/ | ||
public function testContextReturnType( | ||
string $assertType, | ||
string $file, | ||
...$args, | ||
): void | ||
{ | ||
$this->assertFileAsserts($assertType, $file, ...$args); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/TypeSpecifierContextReturnTypeExtension.neon', | ||
]; | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
tests/PHPStan/Analyser/data/TypeSpecifierContextReturnTypeExtension.php
This file contains 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,59 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Tests; | ||
|
||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Analyser\SpecifiedTypes; | ||
use PHPStan\Analyser\TypeSpecifier; | ||
use PHPStan\Analyser\TypeSpecifierAwareExtension; | ||
use PHPStan\Analyser\TypeSpecifierContext; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\MethodTypeSpecifyingExtension; | ||
use TypeSpecifierContextReturnTypeTest\ContextReturnType; | ||
use function str_starts_with; | ||
|
||
class TypeSpecifierContextReturnTypeExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension | ||
{ | ||
|
||
private TypeSpecifier $typeSpecifier; | ||
|
||
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void | ||
{ | ||
$this->typeSpecifier = $typeSpecifier; | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return ContextReturnType::class; | ||
} | ||
|
||
public function isMethodSupported( | ||
MethodReflection $methodReflection, | ||
MethodCall $node, | ||
TypeSpecifierContext $context, | ||
): bool | ||
{ | ||
return str_starts_with($methodReflection->getName(), 'returns'); | ||
} | ||
|
||
public function specifyTypes( | ||
MethodReflection $methodReflection, | ||
MethodCall $node, | ||
Scope $scope, | ||
TypeSpecifierContext $context, | ||
): SpecifiedTypes | ||
{ | ||
if ($context->null()) { | ||
return new SpecifiedTypes(); | ||
} | ||
|
||
return $this->typeSpecifier->create( | ||
$node->getArgs()[0]->value, | ||
$context->getReturnType(), | ||
$context, | ||
$scope, | ||
); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
tests/PHPStan/Analyser/data/type-specifier-context-return-type.php
This file contains 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,29 @@ | ||
<?php | ||
|
||
namespace TypeSpecifierContextReturnTypeTest; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class ContextReturnType { | ||
public function returnsInt(int $specifiedContextReturnType): int {} | ||
|
||
public function doFooLeft(int $i) { | ||
assertType('int', $i); | ||
if ($this->returnsInt($i) > 0) { | ||
staabm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assertType('int<1, max>', $i); | ||
} else { | ||
assertType('int<min, 0>', $i); | ||
} | ||
assertType('int', $i); | ||
} | ||
|
||
public function doFooRight(int $i) { | ||
assertType('int', $i); | ||
if (0 < $this->returnsInt($i)) { | ||
assertType('int<1, max>', $i); | ||
} else { | ||
assertType('int<min, 0>', $i); | ||
} | ||
assertType('int', $i); | ||
} | ||
} |
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.
todo: bit-field cleanup after the main-point of this PR works