Skip to content
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

NullabilityColumnPropertyTypeResolver - default column nullable false #191

Closed
Closed
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
21 changes: 21 additions & 0 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,27 @@ Replace `Doctrine\ORM\Event\LifecycleEventArgs` with specific event classes base

## TypedPropertyFromColumnTypeRector


:wrench: **configure it!**

- class: [`Rector\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector`](../rules/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector.php)

```php
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector;
use Rector\Transform\ValueObject\AttributeKeyToClassConstFetch;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(TypedPropertyFromColumnTypeRector::class, [
TypedPropertyFromColumnTypeRector::DEFAULT_NULLABLE_COLUMN => true,
]);
};
```

Complete `@var` annotations or types based on @ORM\Column

- class: [`Rector\Doctrine\CodeQuality\Rector\Property\TypedPropertyFromColumnTypeRector`](../rules/CodeQuality/Rector/Property/TypedPropertyFromColumnTypeRector.php)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,27 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersion;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Doctrine\NodeManipulator\ColumnPropertyTypeResolver;
use Rector\Doctrine\NodeManipulator\NullabilityColumnPropertyTypeResolver;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\NodeTypeAnalyzer\PropertyTypeDecorator;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Doctrine\Tests\CodeQuality\Rector\Property\TypedPropertyFromColumnTypeRector\TypedPropertyFromColumnTypeRectorTest
*/
final class TypedPropertyFromColumnTypeRector extends AbstractRector implements MinPhpVersionInterface
final class TypedPropertyFromColumnTypeRector extends AbstractRector implements MinPhpVersionInterface, AllowEmptyConfigurableRectorInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AllowEmptyConfigurableRectorInterface is removed at PR:

please use ConfigurableRectorInterface with default empty value instead.

{
public const DEFAULT_NULLABLE_COLUMN = 'default_nullable_column';

private bool $defaultNullableColumn = true;

public function __construct(
private readonly PropertyTypeDecorator $propertyTypeDecorator,
private readonly ColumnPropertyTypeResolver $columnPropertyTypeResolver,
Expand All @@ -38,7 +41,7 @@ public function __construct(
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Complete @var annotations or types based on @ORM\Column', [
new CodeSample(
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
use Doctrine\ORM\Mapping as ORM;

Expand All @@ -62,7 +65,11 @@ class SimpleColumn
private string|null $name = null;
}
CODE_SAMPLE
),
,
[
self::DEFAULT_NULLABLE_COLUMN => true,
]
),
]);
}

Expand All @@ -83,7 +90,7 @@ public function refactor(Node $node): Property|null
return null;
}

$isNullable = $this->nullabilityColumnPropertyTypeResolver->isNullable($node);
$isNullable = $this->nullabilityColumnPropertyTypeResolver->isNullable($node, $this->defaultNullableColumn);

$propertyType = $this->columnPropertyTypeResolver->resolve($node, $isNullable);
if (! $propertyType instanceof Type || $propertyType instanceof MixedType) {
Expand Down Expand Up @@ -116,6 +123,15 @@ public function refactor(Node $node): Property|null
return $node;
}


public function configure(array $configuration): void
{
$defaultNullableColumn = $configuration[self::DEFAULT_NULLABLE_COLUMN] ?? (bool) current($configuration);
Assert::boolean($defaultNullableColumn);

$this->defaultNullableColumn = $defaultNullableColumn;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::TYPED_PROPERTIES;
Expand Down
10 changes: 5 additions & 5 deletions src/NodeManipulator/NullabilityColumnPropertyTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
) {
}

public function isNullable(Property $property): bool
public function isNullable(Property $property, bool $defaultNullableColumn): bool
{
$nullableExpr = $this->attributeFinder->findAttributeByClassArgByName(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I myself are not sure about this (existing) code. This as it threads #[Column(type: ...)], without nullable, as if the attribute isn't set at all. In which case it would test for the @Column annotation. While the existence of the attribute IMO needs to use either the configured nullable or, when absent, the default/fallback, and not looking for the @Column annotation.

$property,
Expand All @@ -44,19 +44,19 @@ public function isNullable(Property $property): bool
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
return $this->isNullableColumn($phpDocInfo);
return $this->isNullableColumn($phpDocInfo, $defaultNullableColumn);
}

private function isNullableColumn(PhpDocInfo $phpDocInfo): bool
private function isNullableColumn(PhpDocInfo $phpDocInfo, bool $defaultNullableColumn): bool
{
$doctrineAnnotationTagValueNode = $phpDocInfo->findOneByAnnotationClass(self::COLUMN_CLASS);
if (! $doctrineAnnotationTagValueNode instanceof DoctrineAnnotationTagValueNode) {
return true;
return $defaultNullableColumn;
}

$nullableValueArrayItemNode = $doctrineAnnotationTagValueNode->getValue('nullable');
if (! $nullableValueArrayItemNode instanceof ArrayItemNode) {
return true;
return $defaultNullableColumn;
}

return $nullableValueArrayItemNode->value instanceof ConstExprTrueNode;
Expand Down
Loading