Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support constant string/int as template bound
- Loading branch information
1 parent
23e95a9
commit 6fe8a46
Showing
9 changed files
with
209 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Generic; | ||
|
||
use PHPStan\Type\Constant\ConstantIntegerType; | ||
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait; | ||
use PHPStan\Type\Type; | ||
|
||
/** @api */ | ||
final class TemplateConstantIntegerType extends ConstantIntegerType implements TemplateType | ||
{ | ||
|
||
/** @use TemplateTypeTrait<ConstantIntegerType> */ | ||
use TemplateTypeTrait; | ||
use UndecidedComparisonCompoundTypeTrait; | ||
|
||
public function __construct( | ||
TemplateTypeScope $scope, | ||
TemplateTypeStrategy $templateTypeStrategy, | ||
TemplateTypeVariance $templateTypeVariance, | ||
string $name, | ||
ConstantIntegerType $bound, | ||
) | ||
{ | ||
parent::__construct($bound->getValue()); | ||
$this->scope = $scope; | ||
$this->strategy = $templateTypeStrategy; | ||
$this->variance = $templateTypeVariance; | ||
$this->name = $name; | ||
$this->bound = $bound; | ||
} | ||
|
||
public function traverse(callable $cb): Type | ||
{ | ||
$newBound = $cb($this->getBound()); | ||
if ($this->getBound() !== $newBound && $newBound instanceof ConstantIntegerType) { | ||
return new self( | ||
$this->scope, | ||
$this->strategy, | ||
$this->variance, | ||
$this->name, | ||
$newBound, | ||
); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
protected function shouldGeneralizeInferredType(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
} |
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,54 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Generic; | ||
|
||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait; | ||
use PHPStan\Type\Type; | ||
|
||
/** @api */ | ||
final class TemplateConstantStringType extends ConstantStringType implements TemplateType | ||
{ | ||
|
||
/** @use TemplateTypeTrait<ConstantStringType> */ | ||
use TemplateTypeTrait; | ||
use UndecidedComparisonCompoundTypeTrait; | ||
|
||
public function __construct( | ||
TemplateTypeScope $scope, | ||
TemplateTypeStrategy $templateTypeStrategy, | ||
TemplateTypeVariance $templateTypeVariance, | ||
string $name, | ||
ConstantStringType $bound, | ||
) | ||
{ | ||
parent::__construct($bound->getValue()); | ||
$this->scope = $scope; | ||
$this->strategy = $templateTypeStrategy; | ||
$this->variance = $templateTypeVariance; | ||
$this->name = $name; | ||
$this->bound = $bound; | ||
} | ||
|
||
public function traverse(callable $cb): Type | ||
{ | ||
$newBound = $cb($this->getBound()); | ||
if ($this->getBound() !== $newBound && $newBound instanceof ConstantStringType) { | ||
return new self( | ||
$this->scope, | ||
$this->strategy, | ||
$this->variance, | ||
$this->name, | ||
$newBound, | ||
); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
protected function shouldGeneralizeInferredType(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
} |
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
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug7381; | ||
|
||
/** | ||
* @template T of array<string, mixed> | ||
*/ | ||
trait AttributeTrait | ||
{ | ||
/** | ||
* @template K of key-of<T> | ||
* @param K $key | ||
* @return T[K]|null | ||
*/ | ||
public function getAttribute(string $key) | ||
{ | ||
return $this->getAttributes()[$key] ?? null; | ||
} | ||
} | ||
|
||
/** | ||
* @phpstan-type Attrs array{foo?: string} | ||
*/ | ||
class Foo { | ||
/** @use AttributeTrait<Attrs> */ | ||
use AttributeTrait; | ||
|
||
/** @return Attrs */ | ||
public function getAttributes(): array | ||
{ | ||
return []; | ||
} | ||
} | ||
|
||
/** | ||
* @phpstan-type Attrs array{foo?: string, bar?: string} | ||
*/ | ||
class Bar { | ||
/** @use AttributeTrait<Attrs> */ | ||
use AttributeTrait; | ||
|
||
/** @return Attrs */ | ||
public function getAttributes(): array | ||
{ | ||
return []; | ||
} | ||
} |
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,17 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace TemplateConstantBound; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
/** | ||
* @template T1 of 'foo' | ||
* @template T2 of 5 | ||
* @param T1 $foo | ||
* @param T2 $bar | ||
*/ | ||
function foo(string $foo, int $bar): void | ||
{ | ||
assertType("T1 of 'foo' (function TemplateConstantBound\\foo(), argument)", $foo); | ||
assertType('T2 of 5 (function TemplateConstantBound\foo(), argument)', $bar); | ||
} |
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 |
---|---|---|
|
@@ -79,3 +79,19 @@ class Dolor | |
{ | ||
|
||
}; | ||
|
||
/** | ||
* @template T of 'string' | ||
*/ | ||
class Sit | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* @template T of 5 | ||
*/ | ||
class Amet | ||
{ | ||
|
||
} |