2
2
3
3
namespace PHPStan \Rules \Doctrine \ORM ;
4
4
5
+ use Doctrine \ORM \Mapping \ClassMetadataInfo ;
5
6
use PHPStan \Reflection \PropertyReflection ;
6
7
use PHPStan \Rules \Properties \ReadWritePropertiesExtension ;
7
8
use PHPStan \Type \Doctrine \ObjectMetadataResolver ;
@@ -47,26 +48,11 @@ public function isAlwaysWritten(PropertyReflection $property, string $propertyNa
47
48
return true ;
48
49
}
49
50
50
- if ($ metadata ->isIdentifierNatural ()) {
51
- return false ;
52
- }
53
-
54
51
if ($ metadata ->versionField === $ propertyName ) {
55
52
return true ;
56
53
}
57
54
58
- try {
59
- $ identifiers = $ metadata ->getIdentifierFieldNames ();
60
- } catch (Throwable $ e ) {
61
- $ mappingException = 'Doctrine\ORM\Mapping\MappingException ' ;
62
- if (!$ e instanceof $ mappingException ) {
63
- throw $ e ;
64
- }
65
-
66
- return false ;
67
- }
68
-
69
- return in_array ($ propertyName , $ identifiers , true );
55
+ return $ this ->isGeneratedIdentifier ($ metadata , $ propertyName );
70
56
}
71
57
72
58
public function isInitialized (PropertyReflection $ property , string $ propertyName ): bool
@@ -82,7 +68,29 @@ public function isInitialized(PropertyReflection $property, string $propertyName
82
68
return false ;
83
69
}
84
70
71
+ if ($ this ->isGeneratedIdentifier ($ metadata , $ propertyName )) {
72
+ return true ;
73
+ }
74
+
85
75
return $ metadata ->isReadOnly && !$ declaringClass ->hasConstructor ();
86
76
}
87
77
78
+ private function isGeneratedIdentifier (ClassMetadataInfo $ metadata , string $ propertyName ): bool
79
+ {
80
+ if ($ metadata ->isIdentifierNatural ()) {
81
+ return false ;
82
+ }
83
+
84
+ try {
85
+ return in_array ($ propertyName , $ metadata ->getIdentifierFieldNames (), true );
86
+ } catch (Throwable $ e ) {
87
+ $ mappingException = 'Doctrine\ORM\Mapping\MappingException ' ;
88
+ if (!$ e instanceof $ mappingException ) {
89
+ throw $ e ;
90
+ }
91
+
92
+ return false ;
93
+ }
94
+ }
95
+
88
96
}
0 commit comments