Skip to content

Allow nullable property typehints for generated value column #75

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

Merged
merged 1 commit into from
Oct 6, 2019
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
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedExceptions"/>
<exclude name="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly"/>
<exclude name="Consistence.Exceptions.ExceptionDeclaration"/>
<exclude name="Squiz.Commenting.FunctionComment.MissingParamTag"/>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses">
<properties>
Expand Down
21 changes: 18 additions & 3 deletions src/Rules/Doctrine/ORM/EntityColumnRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\VerbosityLevel;
use Throwable;
use function sprintf;

class EntityColumnRule implements Rule
Expand Down Expand Up @@ -81,18 +82,32 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$identifier = null;
if ($metadata->generatorType !== 5) { // ClassMetadataInfo::GENERATOR_TYPE_NONE
try {
$identifier = $metadata->getSingleIdentifierFieldName();
} catch (Throwable $e) {
$mappingException = 'Doctrine\ORM\Mapping\MappingException';
if (!$e instanceof $mappingException) {
throw $e;
}
}
}

$writableToPropertyType = $descriptor->getWritableToPropertyType();
$writableToDatabaseType = $descriptor->getWritableToDatabaseType();
if ($fieldMapping['nullable'] === true) {
$nullable = $fieldMapping['nullable'] === true;
if ($nullable) {
$writableToPropertyType = TypeCombinator::addNull($writableToPropertyType);
$writableToDatabaseType = TypeCombinator::addNull($writableToDatabaseType);
}

if (!$property->getWritableType()->isSuperTypeOf($writableToPropertyType)->yes()) {
$errors[] = sprintf('Database can contain %s but property expects %s.', $writableToPropertyType->describe(VerbosityLevel::typeOnly()), $property->getWritableType()->describe(VerbosityLevel::typeOnly()));
}
if (!$writableToDatabaseType->isSuperTypeOf($property->getReadableType())->yes()) {
$errors[] = sprintf('Property can contain %s but database expects %s.', $property->getReadableType()->describe(VerbosityLevel::typeOnly()), $writableToDatabaseType->describe(VerbosityLevel::typeOnly()));
$propertyReadableType = $property->getReadableType();
if (!$writableToDatabaseType->isSuperTypeOf($identifier === $propertyName && !$nullable ? TypeCombinator::removeNull($propertyReadableType) : $propertyReadableType)->yes()) {
$errors[] = sprintf('Property can contain %s but database expects %s.', $propertyReadableType->describe(VerbosityLevel::typeOnly()), $writableToDatabaseType->describe(VerbosityLevel::typeOnly()));
}
return $errors;
}
Expand Down
40 changes: 32 additions & 8 deletions tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Doctrine\ORM;

use Iterator;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\Doctrine\DescriptorRegistry;
Expand Down Expand Up @@ -31,31 +32,30 @@ protected function getRule(): Rule

public function testRule(): void
{
require_once __DIR__ . '/data/MyBrokenSuperclass.php';
$this->analyse([__DIR__ . '/data/MyBrokenEntity.php'], [
[
'Database can contain string but property expects int.',
17,
'Database can contain string but property expects int|null.',
19,
],
[
'Database can contain string|null but property expects string.',
23,
25,
],
[
'Property can contain string|null but database expects string.',
29,
31,
],
[
'Database can contain DateTime but property expects DateTimeImmutable.',
35,
37,
],
[
'Database can contain DateTimeImmutable but property expects DateTime.',
41,
43,
],
[
'Property can contain DateTime but database expects DateTimeImmutable.',
41,
43,
],
]);
}
Expand All @@ -70,4 +70,28 @@ public function testSuperclass(): void
]);
}

/**
* @dataProvider generatedIdsProvider
*/
public function testGeneratedIds(string $file, array $expectedErrors): void
{
$this->analyse([$file], $expectedErrors);
}

public function generatedIdsProvider(): Iterator
{
yield 'not nullable' => [__DIR__ . '/data/GeneratedIdEntity1.php', []];
yield 'nullable column' => [
__DIR__ . '/data/GeneratedIdEntity2.php',
[
[
'Database can contain string|null but property expects string.',
19,
],
],
];
yield 'nullable property' => [__DIR__ . '/data/GeneratedIdEntity3.php', []];
yield 'nullable both' => [__DIR__ . '/data/GeneratedIdEntity4.php', []];
}

}
21 changes: 21 additions & 0 deletions tests/Rules/Doctrine/ORM/data/GeneratedIdEntity1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class GeneratedIdEntity1
{

/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="bigint")
* @var string
*/
private $id;

}
21 changes: 21 additions & 0 deletions tests/Rules/Doctrine/ORM/data/GeneratedIdEntity2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class GeneratedIdEntity2
{

/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="bigint", nullable=true)
* @var string
*/
private $id;

}
21 changes: 21 additions & 0 deletions tests/Rules/Doctrine/ORM/data/GeneratedIdEntity3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class GeneratedIdEntity3
{

/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="bigint")
* @var string|null
*/
private $id;

}
21 changes: 21 additions & 0 deletions tests/Rules/Doctrine/ORM/data/GeneratedIdEntity4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class GeneratedIdEntity4
{

/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="bigint", nullable=true)
* @var string|null
*/
private $id;

}
4 changes: 3 additions & 1 deletion tests/Rules/Doctrine/ORM/data/MyBrokenEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
*/
class MyBrokenEntity extends MyBrokenSuperclass
{

/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="bigint")
* @var int
* @var int|null
*/
private $id;

Expand Down