Skip to content

Assure ORM 3 compatibility with property field mappings #537

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

Closed
wants to merge 3 commits into from
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
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ parameters:
count: 1
path: src/Rules/Doctrine/ORM/EntityRelationRule.php

-
message: "#^Class Doctrine\\\\ORM\\\\Mapping\\\\FieldMapping not found\\.$#"
count: 1
path: src/Rules/Doctrine/ORM/PropertiesExtension.php

-
message: "#^Instanceof between array\\<string, array\\<string, mixed\\>\\|bool\\|int\\|string\\> and Doctrine\\\\ORM\\\\Mapping\\\\FieldMapping will always evaluate to false\\.$#"
count: 1
path: src/Rules/Doctrine/ORM/PropertiesExtension.php

-
message: "#^PHPDoc tag @var with type class\\-string is not subtype of native type 'Doctrine\\\\\\\\Bundle…'\\.$#"
count: 1
Expand Down
13 changes: 11 additions & 2 deletions src/Rules/Doctrine/ORM/PropertiesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\FieldMapping;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Rules\Properties\ReadWritePropertiesExtension;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use Throwable;
use function array_key_exists;
use function in_array;

class PropertiesExtension implements ReadWritePropertiesExtension
Expand Down Expand Up @@ -47,7 +47,16 @@ public function isAlwaysWritten(PropertyReflection $property, string $propertyNa

if (isset($metadata->fieldMappings[$propertyName])) {
$mapping = $metadata->fieldMappings[$propertyName];
if (array_key_exists('generated', $mapping) && $mapping['generated'] !== ClassMetadata::GENERATED_NEVER) {

if ($mapping instanceof FieldMapping) {
// ORM 3
$generated = $mapping->generated;
} else {
// ORM 2
$generated = $mapping['generated'] ?? null;
}

if ($generated !== null && $generated !== ClassMetadata::GENERATED_NEVER) {
return true;
}
}
Expand Down