-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce AccessoryNumericStringType
- Loading branch information
1 parent
7f04f75
commit 1d27c61
Showing
34 changed files
with
384 additions
and
31 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
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,156 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Accessory; | ||
|
||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\CompoundType; | ||
use PHPStan\Type\CompoundTypeHelper; | ||
use PHPStan\Type\Constant\ConstantArrayType; | ||
use PHPStan\Type\Constant\ConstantIntegerType; | ||
use PHPStan\Type\ErrorType; | ||
use PHPStan\Type\FloatType; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\IntersectionType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Traits\NonCallableTypeTrait; | ||
use PHPStan\Type\Traits\NonGenericTypeTrait; | ||
use PHPStan\Type\Traits\NonIterableTypeTrait; | ||
use PHPStan\Type\Traits\NonObjectTypeTrait; | ||
use PHPStan\Type\Traits\UndecidedBooleanTypeTrait; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\UnionType; | ||
|
||
class AccessoryNumericStringType implements CompoundType, AccessoryType | ||
{ | ||
|
||
use NonCallableTypeTrait; | ||
use NonObjectTypeTrait; | ||
use NonIterableTypeTrait; | ||
use UndecidedBooleanTypeTrait; | ||
use NonGenericTypeTrait; | ||
|
||
public function getReferencedClasses(): array | ||
{ | ||
return []; | ||
} | ||
|
||
public function accepts(Type $type, bool $strictTypes): TrinaryLogic | ||
{ | ||
if ($type instanceof CompoundType) { | ||
return CompoundTypeHelper::accepts($type, $this, $strictTypes); | ||
} | ||
|
||
return $type->isNumericString(); | ||
} | ||
|
||
public function isSuperTypeOf(Type $type): TrinaryLogic | ||
{ | ||
if ($this->equals($type)) { | ||
return TrinaryLogic::createYes(); | ||
} | ||
|
||
return $type->isNumericString(); | ||
} | ||
|
||
public function isSubTypeOf(Type $otherType): TrinaryLogic | ||
{ | ||
if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) { | ||
return $otherType->isSuperTypeOf($this); | ||
} | ||
|
||
return $otherType->isNumericString() | ||
->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe()); | ||
} | ||
|
||
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic | ||
{ | ||
return $this->isSubTypeOf($acceptingType); | ||
} | ||
|
||
public function equals(Type $type): bool | ||
{ | ||
return $type instanceof self; | ||
} | ||
|
||
public function describe(\PHPStan\Type\VerbosityLevel $level): string | ||
{ | ||
return 'numeric'; | ||
} | ||
|
||
public function isOffsetAccessible(): TrinaryLogic | ||
{ | ||
return TrinaryLogic::createYes(); | ||
} | ||
|
||
public function hasOffsetValueType(Type $offsetType): TrinaryLogic | ||
{ | ||
return (new IntegerType())->isSuperTypeOf($offsetType)->and(TrinaryLogic::createMaybe()); | ||
} | ||
|
||
public function getOffsetValueType(Type $offsetType): Type | ||
{ | ||
if ($this->hasOffsetValueType($offsetType)->no()) { | ||
return new ErrorType(); | ||
} | ||
|
||
return new StringType(); | ||
} | ||
|
||
public function setOffsetValueType(?Type $offsetType, Type $valueType): Type | ||
{ | ||
return $this; | ||
} | ||
|
||
public function isArray(): TrinaryLogic | ||
{ | ||
return TrinaryLogic::createNo(); | ||
} | ||
|
||
public function toNumber(): Type | ||
{ | ||
return new UnionType([ | ||
$this->toInteger(), | ||
$this->toFloat(), | ||
]); | ||
} | ||
|
||
public function toInteger(): Type | ||
{ | ||
return new IntegerType(); | ||
} | ||
|
||
public function toFloat(): Type | ||
{ | ||
return new FloatType(); | ||
} | ||
|
||
public function toString(): Type | ||
{ | ||
return $this; | ||
} | ||
|
||
public function toArray(): Type | ||
{ | ||
return new ConstantArrayType( | ||
[new ConstantIntegerType(0)], | ||
[$this], | ||
1 | ||
); | ||
} | ||
|
||
public function isNumericString(): TrinaryLogic | ||
{ | ||
return TrinaryLogic::createYes(); | ||
} | ||
|
||
public function traverse(callable $cb): Type | ||
{ | ||
return $this; | ||
} | ||
|
||
public static function __set_state(array $properties): Type | ||
{ | ||
return new self(); | ||
} | ||
|
||
} |
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
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
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
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
Oops, something went wrong.