22
33namespace PHPStan \Rules ;
44
5+ use PHPStan \Reflection \ClassConstantReflection ;
56use PHPStan \Reflection \ExtendedMethodReflection ;
7+ use PHPStan \Reflection \ExtendedPropertyReflection ;
68use function sprintf ;
79use function ucfirst ;
810
@@ -61,34 +63,123 @@ public function getMethod(): ?ExtendedMethodReflection
6163 return $ this ->data ['method ' ] ?? null ;
6264 }
6365
66+ public function getProperty (): ?ExtendedPropertyReflection
67+ {
68+ return $ this ->data ['property ' ] ?? null ;
69+ }
70+
71+ public function getPhpDocTagName (): ?string
72+ {
73+ return $ this ->data ['phpDocTagName ' ] ?? null ;
74+ }
75+
76+ public function getAssertedExprString (): ?string
77+ {
78+ return $ this ->data ['assertedExprString ' ] ?? null ;
79+ }
80+
81+ public function getClassConstant (): ?ClassConstantReflection
82+ {
83+ return $ this ->data ['classConstant ' ] ?? null ;
84+ }
85+
86+ public function getCurrentClassName (): ?string
87+ {
88+ return $ this ->data ['currentClassName ' ] ?? null ;
89+ }
90+
91+ public function getParameterName (): ?string
92+ {
93+ return $ this ->data ['parameterName ' ] ?? null ;
94+ }
95+
96+ public function getTypeAliasName (): ?string
97+ {
98+ return $ this ->data ['typeAliasName ' ] ?? null ;
99+ }
100+
101+ public function getMethodTagName (): ?string
102+ {
103+ return $ this ->data ['methodTagName ' ] ?? null ;
104+ }
105+
106+ public function getPropertyTagName (): ?string
107+ {
108+ return $ this ->data ['propertyTagName ' ] ?? null ;
109+ }
110+
111+ public function getTemplateTagName (): ?string
112+ {
113+ return $ this ->data ['templateTagName ' ] ?? null ;
114+ }
115+
64116 public function createMessage (string $ part ): string
65117 {
66118 switch ($ this ->value ) {
67119 case self ::TRAIT_USE :
68120 return sprintf ('Usage of %s. ' , $ part );
69121 case self ::STATIC_PROPERTY_ACCESS :
122+ $ property = $ this ->getProperty ();
123+ if ($ property !== null ) {
124+ return sprintf ('Access to static property $%s on %s. ' , $ property ->getName (), $ part );
125+ }
126+
70127 return sprintf ('Access to static property on %s. ' , $ part );
71128 case self ::PHPDOC_TAG_ASSERT :
129+ $ phpDocTagName = $ this ->getPhpDocTagName ();
130+ $ assertExprString = $ this ->getAssertedExprString ();
131+ if ($ phpDocTagName !== null && $ assertExprString !== null ) {
132+ return sprintf ('PHPDoc tag %s for %s references %s. ' , $ phpDocTagName , $ assertExprString , $ part );
133+ }
134+
72135 return sprintf ('Assert tag references %s. ' , $ part );
73136 case self ::ATTRIBUTE :
74137 return sprintf ('Attribute references %s. ' , $ part );
75138 case self ::EXCEPTION_CATCH :
76139 return sprintf ('Catching %s. ' , $ part );
77140 case self ::CLASS_CONSTANT_ACCESS :
141+ if ($ this ->getClassConstant () !== null ) {
142+ return sprintf ('Access to constant %s on %s. ' , $ this ->getClassConstant ()->getName (), $ part );
143+ }
78144 return sprintf ('Access to constant on %s. ' , $ part );
79145 case self ::CLASS_IMPLEMENTS :
146+ if ($ this ->getCurrentClassName () !== null ) {
147+ return sprintf ('Class %s implements %s. ' , $ this ->getCurrentClassName (), $ part );
148+ }
149+
80150 return sprintf ('Class implements %s. ' , $ part );
81151 case self ::ENUM_IMPLEMENTS :
152+ if ($ this ->getCurrentClassName () !== null ) {
153+ return sprintf ('Enum %s implements %s. ' , $ this ->getCurrentClassName (), $ part );
154+ }
155+
82156 return sprintf ('Enum implements %s. ' , $ part );
83157 case self ::INTERFACE_EXTENDS :
158+ if ($ this ->getCurrentClassName () !== null ) {
159+ return sprintf ('Interface %s extends %s. ' , $ this ->getCurrentClassName (), $ part );
160+ }
161+
84162 return sprintf ('Interface extends %s. ' , $ part );
85163 case self ::CLASS_EXTENDS :
164+ if ($ this ->getCurrentClassName () !== null ) {
165+ return sprintf ('Class %s extends %s. ' , $ this ->getCurrentClassName (), $ part );
166+ }
167+
86168 return sprintf ('Class extends %s. ' , $ part );
87169 case self ::INSTANCEOF :
88170 return sprintf ('Instanceof references %s. ' , $ part );
89171 case self ::PROPERTY_TYPE :
172+ $ property = $ this ->getProperty ();
173+ if ($ property !== null ) {
174+ return sprintf ('Property $%s references %s in its type. ' , $ property ->getName (), $ part );
175+ }
90176 return sprintf ('Property references %s in its type. ' , $ part );
91177 case self ::PARAMETER_TYPE :
178+ $ parameterName = $ this ->getParameterName ();
179+ if ($ parameterName !== null ) {
180+ return sprintf ('Parameter $%s references %s in its type. ' , $ parameterName , $ part );
181+ }
182+
92183 return sprintf ('Parameter references %s in its type. ' , $ part );
93184 case self ::RETURN_TYPE :
94185 return sprintf ('Return type references %s. ' , $ part );
@@ -99,12 +190,22 @@ public function createMessage(string $part): string
99190 case self ::INSTANTIATION :
100191 return sprintf ('Instantiating %s. ' , $ part );
101192 case self ::TYPE_ALIAS :
193+ if ($ this ->getTypeAliasName () !== null ) {
194+ return sprintf ('Type alias %s references %s. ' , $ this ->getTypeAliasName (), $ part );
195+ }
196+
102197 return sprintf ('Type alias references %s. ' , $ part );
103198 case self ::PHPDOC_TAG_METHOD :
199+ if ($ this ->getMethodTagName () !== null ) {
200+ return sprintf ('PHPDoc tag @method for %s() references %s. ' , $ this ->getMethodTagName (), $ part );
201+ }
104202 return sprintf ('PHPDoc tag @method references %s. ' , $ part );
105203 case self ::PHPDOC_TAG_MIXIN :
106204 return sprintf ('PHPDoc tag @mixin references %s. ' , $ part );
107205 case self ::PHPDOC_TAG_PROPERTY :
206+ if ($ this ->getPropertyTagName () !== null ) {
207+ return sprintf ('PHPDoc tag @property for $%s references %s. ' , $ this ->getPropertyTagName (), $ part );
208+ }
108209 return sprintf ('PHPDoc tag @property references %s. ' , $ part );
109210 case self ::PHPDOC_TAG_REQUIRE_EXTENDS :
110211 return sprintf ('PHPDoc tag @phpstan-require-extends references %s. ' , $ part );
@@ -118,8 +219,16 @@ public function createMessage(string $part): string
118219
119220 return sprintf ('Call to static method on %s. ' , $ part );
120221 case self ::PHPDOC_TAG_TEMPLATE_BOUND :
222+ if ($ this ->getTemplateTagName () !== null ) {
223+ return sprintf ('PHPDoc tag @template %s bound references %s. ' , $ this ->getTemplateTagName (), $ part );
224+ }
225+
121226 return sprintf ('PHPDoc tag @template bound references %s. ' , $ part );
122227 case self ::PHPDOC_TAG_TEMPLATE_DEFAULT :
228+ if ($ this ->getTemplateTagName () !== null ) {
229+ return sprintf ('PHPDoc tag @template %s default references %s. ' , $ this ->getTemplateTagName (), $ part );
230+ }
231+
123232 return sprintf ('PHPDoc tag @template default references %s. ' , $ part );
124233 }
125234 }
0 commit comments