1212namespace PhpCsFixerCustomFixers \Fixer ;
1313
1414use PhpCsFixer \Fixer \ConfigurableFixerInterface ;
15+ use PhpCsFixer \Fixer \DeprecatedFixerInterface ;
16+ use PhpCsFixer \Fixer \FunctionNotation \MultilinePromotedPropertiesFixer as PhpCsFixerMultilinePromotedPropertiesFixer ;
1517use PhpCsFixer \Fixer \WhitespacesAwareFixerInterface ;
16- use PhpCsFixer \FixerConfiguration \FixerConfigurationResolver ;
1718use PhpCsFixer \FixerConfiguration \FixerConfigurationResolverInterface ;
18- use PhpCsFixer \FixerConfiguration \FixerOptionBuilder ;
19- use PhpCsFixer \FixerDefinition \FixerDefinition ;
2019use PhpCsFixer \FixerDefinition \FixerDefinitionInterface ;
21- use PhpCsFixer \FixerDefinition \VersionSpecification ;
22- use PhpCsFixer \FixerDefinition \VersionSpecificCodeSample ;
23- use PhpCsFixer \Tokenizer \Analyzer \WhitespacesAnalyzer ;
24- use PhpCsFixer \Tokenizer \CT ;
2520use PhpCsFixer \Tokenizer \Tokens ;
2621use PhpCsFixer \WhitespacesFixerConfig ;
27- use PhpCsFixerCustomFixers \Analyzer \ConstructorAnalyzer ;
2822
2923/**
24+ * @deprecated
25+ *
3026 * @implements ConfigurableFixerInterface<_InputConfig, _Config>
3127 *
3228 * @phpstan-type _InputConfig array{keep_blank_lines?: bool, minimum_number_of_parameters?: int}
3329 * @phpstan-type _Config array{keep_blank_lines: bool, minimum_number_of_parameters: int}
3430 *
3531 * @no-named-arguments
3632 */
37- final class MultilinePromotedPropertiesFixer extends AbstractFixer implements ConfigurableFixerInterface, WhitespacesAwareFixerInterface
33+ final class MultilinePromotedPropertiesFixer extends AbstractFixer implements ConfigurableFixerInterface, DeprecatedFixerInterface, WhitespacesAwareFixerInterface
3834{
39- private int $ minimumNumberOfParameters = 1 ;
40- private bool $ keepBlankLines = false ;
41- private WhitespacesFixerConfig $ whitespacesConfig ;
35+ private PhpCsFixerMultilinePromotedPropertiesFixer $ multilinePromotedPropertiesFixer ;
36+
37+ public function __construct ()
38+ {
39+ $ this ->multilinePromotedPropertiesFixer = new PhpCsFixerMultilinePromotedPropertiesFixer ();
40+ }
4241
4342 public function getDefinition (): FixerDefinitionInterface
4443 {
45- return new FixerDefinition (
46- 'Promoted properties must be on separate lines. ' ,
47- [
48- new VersionSpecificCodeSample (
49- '<?php class Foo {
50- public function __construct(private array $a, private bool $b, private int $i) {}
51- }
52- ' ,
53- new VersionSpecification (80000 ),
54- ),
55- ],
56- '' ,
57- );
44+ return $ this ->multilinePromotedPropertiesFixer ->getDefinition ();
5845 }
5946
6047 public function getConfigurationDefinition (): FixerConfigurationResolverInterface
6148 {
62- return new FixerConfigurationResolver ([
63- (new FixerOptionBuilder ('keep_blank_lines ' , 'whether to keep blank lines between properties ' ))
64- ->setAllowedTypes (['bool ' ])
65- ->setDefault ($ this ->keepBlankLines )
66- ->getOption (),
67- (new FixerOptionBuilder ('minimum_number_of_parameters ' , 'minimum number of parameters in the constructor to fix ' ))
68- ->setAllowedTypes (['int ' ])
69- ->setDefault ($ this ->minimumNumberOfParameters )
70- ->getOption (),
71- ]);
49+ return $ this ->multilinePromotedPropertiesFixer ->getConfigurationDefinition ();
7250 }
7351
7452 /**
7553 * @param array{minimum_number_of_parameters?: int, keep_blank_lines?: bool} $configuration
7654 */
7755 public function configure (array $ configuration ): void
7856 {
79- if (\array_key_exists ('minimum_number_of_parameters ' , $ configuration )) {
80- $ this ->minimumNumberOfParameters = $ configuration ['minimum_number_of_parameters ' ];
81- }
82- if (\array_key_exists ('keep_blank_lines ' , $ configuration )) {
83- $ this ->keepBlankLines = $ configuration ['keep_blank_lines ' ];
84- }
57+ $ this ->multilinePromotedPropertiesFixer ->configure ($ configuration );
8558 }
8659
8760 public function setWhitespacesConfig (WhitespacesFixerConfig $ config ): void
8861 {
89- $ this ->whitespacesConfig = $ config ;
62+ $ this ->multilinePromotedPropertiesFixer -> setWhitespacesConfig ( $ config) ;
9063 }
9164
9265 /**
@@ -95,112 +68,29 @@ public function setWhitespacesConfig(WhitespacesFixerConfig $config): void
9568 */
9669 public function getPriority (): int
9770 {
98- return 0 ;
71+ return $ this -> multilinePromotedPropertiesFixer -> getPriority () ;
9972 }
10073
10174 public function isCandidate (Tokens $ tokens ): bool
10275 {
103- return $ tokens ->isAnyTokenKindsFound ([
104- CT ::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE ,
105- CT ::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED ,
106- CT ::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC ,
107- ]);
76+ return $ this ->multilinePromotedPropertiesFixer ->isCandidate ($ tokens );
10877 }
10978
11079 public function isRisky (): bool
11180 {
112- return false ;
81+ return $ this -> multilinePromotedPropertiesFixer -> isRisky () ;
11382 }
11483
11584 public function fix (\SplFileInfo $ file , Tokens $ tokens ): void
11685 {
117- $ constructorAnalyzer = new ConstructorAnalyzer ();
118-
119- for ($ index = $ tokens ->count () - 1 ; $ index > 0 ; $ index --) {
120- if (!$ tokens [$ index ]->isGivenKind (\T_CLASS )) {
121- continue ;
122- }
123-
124- $ constructorAnalysis = $ constructorAnalyzer ->findNonAbstractConstructor ($ tokens , $ index );
125- if ($ constructorAnalysis === null ) {
126- continue ;
127- }
128-
129- $ openParenthesis = $ tokens ->getNextTokenOfKind ($ constructorAnalysis ->getConstructorIndex (), ['( ' ]);
130- \assert (\is_int ($ openParenthesis ));
131- $ closeParenthesis = $ tokens ->findBlockEnd (Tokens::BLOCK_TYPE_PARENTHESIS_BRACE , $ openParenthesis );
132-
133- if (!$ this ->shouldBeFixed ($ tokens , $ openParenthesis , $ closeParenthesis )) {
134- continue ;
135- }
136-
137- $ this ->fixParameters ($ tokens , $ openParenthesis , $ closeParenthesis );
138- }
139- }
140-
141- private function shouldBeFixed (Tokens $ tokens , int $ openParenthesis , int $ closeParenthesis ): bool
142- {
143- $ promotedParameterFound = false ;
144- $ minimumNumberOfParameters = 0 ;
145- for ($ index = $ openParenthesis + 1 ; $ index < $ closeParenthesis ; $ index ++) {
146- if ($ tokens [$ index ]->isGivenKind (\T_VARIABLE )) {
147- $ minimumNumberOfParameters ++;
148- }
149- if (
150- $ tokens [$ index ]->isGivenKind ([
151- CT ::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE ,
152- CT ::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED ,
153- CT ::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC ,
154- ])
155- ) {
156- $ promotedParameterFound = true ;
157- }
158- }
159-
160- return $ promotedParameterFound && $ minimumNumberOfParameters >= $ this ->minimumNumberOfParameters ;
86+ $ this ->multilinePromotedPropertiesFixer ->fix ($ file , $ tokens );
16187 }
16288
163- private function fixParameters (Tokens $ tokens , int $ openParenthesis , int $ closeParenthesis ): void
164- {
165- $ indent = WhitespacesAnalyzer::detectIndent ($ tokens , $ openParenthesis );
166-
167- $ tokens ->ensureWhitespaceAtIndex (
168- $ closeParenthesis - 1 ,
169- 1 ,
170- $ this ->whitespacesConfig ->getLineEnding () . $ indent ,
171- );
172-
173- $ index = $ tokens ->getPrevMeaningfulToken ($ closeParenthesis );
174- \assert (\is_int ($ index ));
175-
176- while ($ index > $ openParenthesis ) {
177- $ index = $ tokens ->getPrevMeaningfulToken ($ index );
178- \assert (\is_int ($ index ));
179-
180- $ blockType = Tokens::detectBlockType ($ tokens [$ index ]);
181- if ($ blockType !== null && !$ blockType ['isStart ' ]) {
182- $ index = $ tokens ->findBlockStart ($ blockType ['type ' ], $ index );
183- continue ;
184- }
185-
186- if (!$ tokens [$ index ]->equalsAny (['( ' , ', ' ])) {
187- continue ;
188- }
189-
190- $ this ->fixParameter ($ tokens , $ index + 1 , $ indent );
191- }
192- }
193-
194- private function fixParameter (Tokens $ tokens , int $ index , string $ indent ): void
89+ /**
90+ * @return list<string>
91+ */
92+ public function getSuccessorsNames (): array
19593 {
196- if ($ this ->keepBlankLines && $ tokens [$ index ]->isWhitespace () && \str_contains ($ tokens [$ index ]->getContent (), "\n" )) {
197- return ;
198- }
199-
200- $ tokens ->ensureWhitespaceAtIndex (
201- $ index ,
202- 0 ,
203- $ this ->whitespacesConfig ->getLineEnding () . $ indent . $ this ->whitespacesConfig ->getIndent (),
204- );
94+ return [$ this ->multilinePromotedPropertiesFixer ->getName ()];
20595 }
20696}
0 commit comments