Skip to content

Commit

Permalink
cs
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 23, 2023
1 parent 2afa47c commit 079cd51
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
15 changes: 7 additions & 8 deletions packages/PHPStanStaticTypeMapper/DoctrineTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,22 @@ public function isDoctrineCollectionWithIterableUnionType(Type $type): bool
return $arrayType instanceof ArrayType;
}

private function isCollectionObjectType(Type $type): bool
public function isInstanceOfCollectionType(Type $type): bool
{
if (! $type instanceof TypeWithClassName) {
if (! $type instanceof ObjectType) {
return false;
}

return $type->getClassName() === 'Doctrine\Common\Collections\Collection';
return $type->isInstanceOf('Doctrine\Common\Collections\Collection')
->yes();
}

public function isInstanceOfCollectionType(Type $type): bool
private function isCollectionObjectType(Type $type): bool
{
if (! $type instanceof ObjectType) {
if (! $type instanceof TypeWithClassName) {
return false;
}

return $type->isInstanceOf('Doctrine\Common\Collections\Collection')
->yes();
return $type->getClassName() === 'Doctrine\Common\Collections\Collection';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorReadonlyClassRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(TypedPropertyFromStrictConstructorReadonlyClassRector::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@
namespace Rector\TypeDeclaration\Rector\Property;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\MethodName;
Expand Down Expand Up @@ -51,7 +46,9 @@ public function __construct(

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add typed public properties based only on strict constructor types in readonly classes', [
return new RuleDefinition(
'Add typed public properties based only on strict constructor types in readonly classes',
[
new CodeSample(
<<<'CODE_SAMPLE'
/**
Expand Down Expand Up @@ -84,6 +81,7 @@ public function __construct(string $name)
}
CODE_SAMPLE
),

]);
}

Expand Down Expand Up @@ -144,7 +142,10 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
$hasChanged = true;
}

if ($this->propertyTypeDefaultValueAnalyzer->doesConflictWithDefaultValue($propertyProperty, $propertyType)) {
if ($this->propertyTypeDefaultValueAnalyzer->doesConflictWithDefaultValue(
$propertyProperty,
$propertyType
)) {
continue;
}

Expand All @@ -166,9 +167,13 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::TYPED_PROPERTIES;
}

private function shouldSkipProperty(Property $property, Type $propertyType, ClassReflection $classReflection, Scope $scope): bool
{
if (!$property->isPublic()) {
private function shouldSkipProperty(
Property $property,
Type $propertyType,
ClassReflection $classReflection,
Scope $scope
): bool {
if (! $property->isPublic()) {
return true;
}

Expand All @@ -190,7 +195,7 @@ private function shouldSkipProperty(Property $property, Type $propertyType, Clas
}
}

if (!$isReadOnlyByPhpdoc) {
if (! $isReadOnlyByPhpdoc) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
namespace Rector\TypeDeclaration\Rector\Property;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\PropertyProperty;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -142,7 +139,10 @@ public function refactor(Node $node): ?Node
$hasChanged = true;
}

if ($this->propertyTypeDefaultValueAnalyzer->doesConflictWithDefaultValue($propertyProperty, $propertyType)) {
if ($this->propertyTypeDefaultValueAnalyzer->doesConflictWithDefaultValue(
$propertyProperty,
$propertyType
)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
use PHPStan\Type\Type;
use Rector\StaticTypeMapper\StaticTypeMapper;

final class PropertyTypeDefaultValueAnalyzer {
final class PropertyTypeDefaultValueAnalyzer
{
public function __construct(
private readonly StaticTypeMapper $staticTypeMapper
)
{}
) {
}

public function doesConflictWithDefaultValue(PropertyProperty $propertyProperty, Type $propertyType): bool
{
Expand Down

0 comments on commit 079cd51

Please sign in to comment.