File tree Expand file tree Collapse file tree 4 files changed +123
-0
lines changed Expand file tree Collapse file tree 4 files changed +123
-0
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ services:
88        class :  PHPStan\Rules\Deprecations\OverrideDeprecatedPropertyRule 
99    - 
1010        class :  PHPStan\Rules\Deprecations\OverrideDeprecatedMethodRule 
11+     - 
12+         class :  PHPStan\Rules\Deprecations\OverrideDeprecatedConstantRule 
1113
1214rules : 
1315	- PHPStan\Rules\Deprecations\AccessDeprecatedPropertyRule 
@@ -32,3 +34,5 @@ conditionalTags:
3234        phpstan.rules.rule :  %featureToggles.bleedingEdge% 
3335    PHPStan\Rules\Deprecations\OverrideDeprecatedMethodRule : 
3436        phpstan.rules.rule :  %featureToggles.bleedingEdge% 
37+     PHPStan\Rules\Deprecations\OverrideDeprecatedConstantRule : 
38+         phpstan.rules.rule :  %featureToggles.bleedingEdge% 
Original file line number Diff line number Diff line change 1+ <?php  declare (strict_types = 1 );
2+ 
3+ namespace  PHPStan \Rules \Deprecations ;
4+ 
5+ use  PhpParser \Node ;
6+ use  PhpParser \Node \Stmt \ClassConst ;
7+ use  PHPStan \Analyser \Scope ;
8+ use  PHPStan \Rules \Rule ;
9+ use  function  sprintf ;
10+ 
11+ /** 
12+  * @implements Rule<ClassConst> 
13+  */ 
14+ class  OverrideDeprecatedConstantRule implements  Rule
15+ {
16+ 
17+ 	public  function  getNodeType (): string 
18+ 	{
19+ 		return  ClassConst::class;
20+ 	}
21+ 
22+ 	public  function  processNode (Node   $ node , Scope   $ scope ): array 
23+ 	{
24+ 		if  (DeprecatedScopeHelper::isScopeDeprecated ($ scope )) {
25+ 			return  [];
26+ 		}
27+ 
28+ 		if  (!$ scope ->isInClass ()) {
29+ 			return  [];
30+ 		}
31+ 
32+ 		if  ($ node ->isPrivate ()) {
33+ 			return  [];
34+ 		}
35+ 
36+ 		$ class  = $ scope ->getClassReflection ();
37+ 
38+ 		$ parents  = $ class ->getParents ();
39+ 
40+ 		$ name  = (string ) $ node ->consts [0 ]->name ;
41+ 
42+ 		foreach  ($ parents  as  $ parent ) {
43+ 			if  (!$ parent ->hasConstant ($ name )) {
44+ 				continue ;
45+ 			}
46+ 
47+ 			$ parentConst  = $ parent ->getConstant ($ name );
48+ 
49+ 			if  (!$ parentConst ->isDeprecated ()->yes ()) {
50+ 				return  [];
51+ 			}
52+ 
53+ 			return  [sprintf (
54+ 				'Class %s overrides deprecated const %s of class %s. ' ,
55+ 				$ class ->getName (),
56+ 				$ name ,
57+ 				$ parent ->getName ()
58+ 			)];
59+ 		}
60+ 
61+ 		return  [];
62+ 	}
63+ 
64+ }
Original file line number Diff line number Diff line change 1+ <?php  declare (strict_types = 1 );
2+ 
3+ namespace  PHPStan \Rules \Deprecations ;
4+ 
5+ use  PHPStan \Rules \Rule ;
6+ use  PHPStan \Testing \RuleTestCase ;
7+ 
8+ /** 
9+  * @extends RuleTestCase<OverrideDeprecatedConstantRule> 
10+  */ 
11+ class  OverrideDeprecatedConstantRuleTest extends  RuleTestCase
12+ {
13+ 
14+ 	protected  function  getRule (): Rule 
15+ 	{
16+ 		return  new  OverrideDeprecatedConstantRule ();
17+ 	}
18+ 
19+ 	public  function  testDeprecatedConstantOverride (): void 
20+ 	{
21+ 		$ this  ->analyse (
22+ 			[__DIR__  . '/data/override-deprecated-constant.php ' ],
23+ 			[
24+ 				[
25+ 					'Class OverrideDeprecatedConstant\Child overrides deprecated const FOO of class OverrideDeprecatedConstant\Foo. ' ,
26+ 					20 ,
27+ 				],
28+ 			]
29+ 		);
30+ 	}
31+ 
32+ }
Original file line number Diff line number Diff line change 1+ <?php 
2+ 
3+ namespace  OverrideDeprecatedConstant ;
4+ 
5+ class  Foo
6+ {
7+ 	/** 
8+ 	 * @deprecated 
9+ 	 */ 
10+ 	public  const  FOO  = '' ;
11+ 
12+ 	/** 
13+ 	 * @deprecated 
14+ 	 */ 
15+ 	private  const  BAR  = '' ;
16+ }
17+ 
18+ class  Child extends  Foo
19+ {
20+ 	public  const  FOO  = '' ;
21+ 
22+ 	private  const  BAR  = '' ;
23+ }
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments