Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Union types in properties
  • Loading branch information
ondrejmirtes committed Dec 19, 2021
commit d1524be6e1d4dd8ab7e32a1e5950275f1d3abd34
2 changes: 2 additions & 0 deletions build/rector-downgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rector\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector;
use Rector\DowngradePhp80\Rector\Catch_\DowngradeNonCapturingCatchesRector;
use Rector\DowngradePhp80\Rector\ClassMethod\DowngradeTrailingCommasInParamUseRector;
use Rector\DowngradePhp80\Rector\Property\DowngradeUnionTypeTypedPropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;


Expand Down Expand Up @@ -38,6 +39,7 @@
if ($targetPhpVersionId < 80000) {
$services->set(DowngradeTrailingCommasInParamUseRector::class);
$services->set(DowngradeNonCapturingCatchesRector::class);
$services->set(DowngradeUnionTypeTypedPropertyRector::class);
}

if ($targetPhpVersionId < 70400) {
Expand Down
14 changes: 14 additions & 0 deletions patches/TypeFactory.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@package rector/rector

--- packages/NodeTypeResolver/PHPStan/Type/TypeFactory.php 2021-11-23 18:38:29.000000000 +0100
+++ packages/NodeTypeResolver/PHPStan/Type/TypeFactory.php 2021-12-18 22:49:01.000000000 +0100
@@ -122,9 +122,6 @@
if ($type instanceof \PHPStan\Type\Constant\ConstantIntegerType) {
return new \PHPStan\Type\IntegerType();
}
- if ($type instanceof \PHPStan\Type\Constant\ConstantBooleanType) {
- return new \PHPStan\Type\BooleanType();
- }
return $type;
}
/**
8 changes: 8 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification"/>
<properties>
<property name="enableNativeTypeHint" value="true"/>
<property name="enableUnionTypeHint" value="true"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint.UselessAnnotation">
Expand Down Expand Up @@ -157,6 +158,13 @@
<property name="enable" value="true"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.UnionTypeHintFormat">
<properties>
<property name="enable" value="true"/>
<property name="withSpaces" value="no"/>
<property name="nullPosition" value="last"/>
</properties>
</rule>
<exclude-pattern>tests/*/data</exclude-pattern>
<exclude-pattern>tests/e2e/resultCache_1.php</exclude-pattern>
<exclude-pattern>tests/e2e/resultCache_2.php</exclude-pattern>
Expand Down
3 changes: 1 addition & 2 deletions src/Analyser/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class Error implements JsonSerializable

private ?int $line;

/** @var bool|Throwable */
private $canBeIgnored;
private bool|Throwable $canBeIgnored;

private ?string $filePath;

Expand Down
3 changes: 1 addition & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ class MutatingScope implements Scope
/** @var array<string, Type> */
private array $constantTypes;

/** @var FunctionReflection|MethodReflection|null */
private $function;
private FunctionReflection|MethodReflection|null $function = null;

private ?string $namespace;

Expand Down
3 changes: 1 addition & 2 deletions src/Node/BooleanAndNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
class BooleanAndNode extends NodeAbstract implements VirtualNode
{

/** @var BooleanAnd|LogicalAnd */
private $originalNode;
private BooleanAnd|LogicalAnd $originalNode;

private Scope $rightScope;

Expand Down
3 changes: 1 addition & 2 deletions src/Node/BooleanOrNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
class BooleanOrNode extends NodeAbstract implements VirtualNode
{

/** @var BooleanOr|LogicalOr */
private $originalNode;
private BooleanOr|LogicalOr $originalNode;

private Scope $rightScope;

Expand Down
3 changes: 1 addition & 2 deletions src/Node/ClassPropertyNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class ClassPropertyNode extends NodeAbstract implements VirtualNode

private int $flags;

/** @var Identifier|Name|Node\ComplexType|null */
private $type;
private Identifier|Name|Node\ComplexType|null $type = null;

private ?Expr $default;

Expand Down
3 changes: 1 addition & 2 deletions src/Node/FunctionCallableNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
class FunctionCallableNode extends Expr implements VirtualNode
{

/** @var Name|Expr */
private $name;
private Name|Expr $name;

/**
* @param Expr|Name $name
Expand Down
3 changes: 1 addition & 2 deletions src/Node/InstantiationCallableNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
class InstantiationCallableNode extends Expr implements VirtualNode
{

/** @var Name|Expr */
private $class;
private Name|Expr $class;

/**
* @param Expr|Name $class
Expand Down
3 changes: 1 addition & 2 deletions src/Node/Method/MethodCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
class MethodCall
{

/** @var Node\Expr\MethodCall|StaticCall|Array_ */
private $node;
private Node\Expr\MethodCall|StaticCall|Array_ $node;

private Scope $scope;

Expand Down
3 changes: 1 addition & 2 deletions src/Node/MethodCallableNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class MethodCallableNode extends Expr implements VirtualNode

private Expr $var;

/** @var Identifier|Expr */
private $name;
private Identifier|Expr $name;

private Expr\MethodCall $originalNode;

Expand Down
3 changes: 1 addition & 2 deletions src/Node/Property/PropertyRead.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
class PropertyRead
{

/** @var PropertyFetch|StaticPropertyFetch */
private $fetch;
private PropertyFetch|StaticPropertyFetch $fetch;

private Scope $scope;

Expand Down
3 changes: 1 addition & 2 deletions src/Node/Property/PropertyWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
class PropertyWrite
{

/** @var PropertyFetch|StaticPropertyFetch */
private $fetch;
private PropertyFetch|StaticPropertyFetch $fetch;

private Scope $scope;

Expand Down
6 changes: 2 additions & 4 deletions src/Node/StaticMethodCallableNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
class StaticMethodCallableNode extends Expr implements VirtualNode
{

/** @var Name|Expr */
private $class;
private Name|Expr $class;

/** @var Identifier|Expr */
private $name;
private Identifier|Expr $name;

private Expr\StaticCall $originalNode;

Expand Down
38 changes: 18 additions & 20 deletions src/PhpDoc/ResolvedPhpDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PHPStan\Type\Generic\TemplateTypeMap;
use function array_key_exists;
use function count;
use function is_bool;

/** @api */
class ResolvedPhpDocBlock
Expand All @@ -47,43 +48,40 @@ class ResolvedPhpDocBlock
private PhpDocNodeResolver $phpDocNodeResolver;

/** @var array<(string|int), VarTag>|false */
private $varTags = false;
private array|false $varTags = false;

/** @var array<string, MethodTag>|false */
private $methodTags = false;
private array|false $methodTags = false;

/** @var array<string, PropertyTag>|false */
private $propertyTags = false;
private array|false $propertyTags = false;

/** @var array<string, ExtendsTag>|false */
private $extendsTags = false;
private array|false $extendsTags = false;

/** @var array<string, ImplementsTag>|false */
private $implementsTags = false;
private array|false $implementsTags = false;

/** @var array<string, UsesTag>|false */
private $usesTags = false;
private array|false $usesTags = false;

/** @var array<string, ParamTag>|false */
private $paramTags = false;
private array|false $paramTags = false;

/** @var ReturnTag|false|null */
private $returnTag = false;
private ReturnTag|false|null $returnTag = false;

/** @var ThrowsTag|false|null */
private $throwsTag = false;
private ThrowsTag|false|null $throwsTag = false;

/** @var array<MixinTag>|false */
private $mixinTags = false;
private array|false $mixinTags = false;

/** @var array<TypeAliasTag>|false */
private $typeAliasTags = false;
private array|false $typeAliasTags = false;

/** @var array<TypeAliasImportTag>|false */
private $typeAliasImportTags = false;
private array|false $typeAliasImportTags = false;

/** @var DeprecatedTag|false|null */
private $deprecatedTag = false;
private DeprecatedTag|false|null $deprecatedTag = false;

private ?bool $isDeprecated = null;

Expand All @@ -92,7 +90,7 @@ class ResolvedPhpDocBlock
private ?bool $isFinal = null;

/** @var bool|'notLoaded'|null */
private $isPure = 'notLoaded';
private bool|string|null $isPure = 'notLoaded';

private function __construct()
{
Expand Down Expand Up @@ -380,7 +378,7 @@ public function getParamTags(): array

public function getReturnTag(): ?ReturnTag
{
if ($this->returnTag === false) {
if (is_bool($this->returnTag)) {
$this->returnTag = $this->phpDocNodeResolver->resolveReturnTag(
$this->phpDocNode,
$this->getNameScope(),
Expand All @@ -391,7 +389,7 @@ public function getReturnTag(): ?ReturnTag

public function getThrowsTag(): ?ThrowsTag
{
if ($this->throwsTag === false) {
if (is_bool($this->throwsTag)) {
$this->throwsTag = $this->phpDocNodeResolver->resolveThrowsTags(
$this->phpDocNode,
$this->getNameScope(),
Expand Down Expand Up @@ -447,7 +445,7 @@ public function getTypeAliasImportTags(): array

public function getDeprecatedTag(): ?DeprecatedTag
{
if ($this->deprecatedTag === false) {
if (is_bool($this->deprecatedTag)) {
$this->deprecatedTag = $this->phpDocNodeResolver->resolveDeprecatedTag(
$this->phpDocNode,
$this->getNameScope(),
Expand Down
16 changes: 7 additions & 9 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use function count;
use function implode;
use function in_array;
use function is_bool;
use function is_file;
use function method_exists;
use function reset;
Expand Down Expand Up @@ -113,17 +114,14 @@ class ClassReflection
/** @var array<string, bool> */
private array $subclasses = [];

/** @var string|false|null */
private $filename = false;
private string|false|null $filename = false;

/** @var string|false|null */
private $reflectionDocComment = false;
private string|false|null $reflectionDocComment = false;

/** @var ClassReflection[]|null */
private ?array $cachedInterfaces = null;

/** @var ClassReflection|false|null */
private $cachedParentClass = false;
private ClassReflection|false|null $cachedParentClass = false;

/** @var array<string, TypeAlias>|null */
private ?array $typeAliases = null;
Expand Down Expand Up @@ -173,7 +171,7 @@ public function getNativeReflection(): ReflectionClass

public function getFileName(): ?string
{
if ($this->filename !== false) {
if (!is_bool($this->filename)) {
return $this->filename;
}

Expand Down Expand Up @@ -203,7 +201,7 @@ public function getFileNameWithPhpDocs(): ?string

public function getParentClass(): ?ClassReflection
{
if ($this->cachedParentClass !== false) {
if (!is_bool($this->cachedParentClass)) {
return $this->cachedParentClass;
}

Expand Down Expand Up @@ -1098,7 +1096,7 @@ public function getResolvedPhpDoc(): ?ResolvedPhpDocBlock
return null;
}

if ($this->reflectionDocComment === false) {
if (is_bool($this->reflectionDocComment)) {
$docComment = $this->reflection->getDocComment();
$this->reflectionDocComment = $docComment !== false ? $docComment : null;
}
Expand Down