@@ -31,6 +31,11 @@ function foo($a, $b, $c) {}
3131 );
3232 }
3333
34+ public function getPriority (): int
35+ {
36+ return 5 ;
37+ }
38+
3439 public function isCandidate (Tokens $ tokens ): bool
3540 {
3641 return $ tokens ->isAllTokenKindsFound ([T_DOC_COMMENT , T_FUNCTION ]);
@@ -55,21 +60,27 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
5560
5661 $ paramNames = $ this ->getParamNames ($ tokens , $ functionIndex );
5762
58- $ newContent = $ this ->getSortedDocComment ($ tokens [$ index ]->getContent (), $ paramNames );
63+ $ docBlock = new DocBlock ($ tokens [$ index ]->getContent ());
64+ $ sorted = $ this ->getSortedAnnotations ($ docBlock ->getAnnotations (), $ paramNames );
65+
66+ foreach ($ sorted as $ annotationIndex => $ annotationContent ) {
67+ /** @var Annotation $annotation */
68+ $ annotation = $ docBlock ->getAnnotation ($ annotationIndex );
69+ $ annotation ->remove ();
5970
60- if ($ newContent === $ tokens [$ index ]->getContent ()) {
71+ /** @var Line $line */
72+ $ line = $ docBlock ->getLine ($ annotation ->getStart ());
73+ $ line ->setContent ($ annotationContent );
74+ }
75+
76+ if ($ docBlock ->getContent () === $ tokens [$ index ]->getContent ()) {
6177 continue ;
6278 }
6379
64- $ tokens [$ index ] = new Token ([T_DOC_COMMENT , $ newContent ]);
80+ $ tokens [$ index ] = new Token ([T_DOC_COMMENT , $ docBlock -> getContent () ]);
6581 }
6682 }
6783
68- public function getPriority (): int
69- {
70- return 5 ;
71- }
72-
7384 /**
7485 * @return string[]
7586 */
@@ -90,25 +101,17 @@ private function getParamNames(Tokens $tokens, int $functionIndex): array
90101 return $ paramNames ;
91102 }
92103
93- private function getSortedDocComment ( string $ comment , array $ paramNames ): string
104+ private function getSortedAnnotations ( array $ annotations , array $ paramNames ): array
94105 {
95- $ docBlock = new DocBlock ($ comment );
96- $ firstParamIndex = null ;
106+ $ paramFound = false ;
97107 $ annotationsBeforeParams = [];
98108 $ paramsByName = \array_combine ($ paramNames , \array_fill (0 , \count ($ paramNames ), null ));
99109 $ superfluousParams = [];
100110 $ annotationsAfterParams = [];
101111
102- foreach ($ docBlock ->getAnnotations () as $ index => $ annotation ) {
103- if ($ firstParamIndex === null ) {
104- if ($ annotation ->getTag ()->getName () !== 'param ' ) {
105- $ annotationsBeforeParams [] = $ annotation ->getContent ();
106- continue ;
107- }
108- $ firstParamIndex = $ index ;
109- }
110-
112+ foreach ($ annotations as $ annotation ) {
111113 if ($ annotation ->getTag ()->getName () === 'param ' ) {
114+ $ paramFound = true ;
112115 foreach ($ paramNames as $ paramName ) {
113116 if (Preg::match (\sprintf ('/@param\s+(?:[^\$](?:[^<\s]|<[^>]*>)*\s+)?(?:&|\.\.\.)?\s*(\Q%s\E)\b/ ' , $ paramName ), $ annotation ->getContent (), $ matches ) === 1 && !isset ($ paramsByName [$ matches [1 ]])) {
114117 $ paramsByName [$ matches [1 ]] = $ annotation ->getContent ();
@@ -119,21 +122,14 @@ private function getSortedDocComment(string $comment, array $paramNames): string
119122 continue ;
120123 }
121124
122- $ annotationsAfterParams [] = $ annotation ->getContent ();
123- }
124-
125- $ sorted = \array_merge ($ annotationsBeforeParams , \array_values (\array_filter ($ paramsByName )), $ superfluousParams , $ annotationsAfterParams );
126-
127- foreach ($ sorted as $ index => $ annotationContent ) {
128- /** @var Annotation $annotation */
129- $ annotation = $ docBlock ->getAnnotation ($ index );
130- $ annotation ->remove ();
125+ if ($ paramFound ) {
126+ $ annotationsAfterParams [] = $ annotation ->getContent ();
127+ continue ;
128+ }
131129
132- /** @var Line $line */
133- $ line = $ docBlock ->getLine ($ annotation ->getStart ());
134- $ line ->setContent ($ annotationContent );
130+ $ annotationsBeforeParams [] = $ annotation ->getContent ();
135131 }
136132
137- return $ docBlock -> getContent ( );
133+ return \array_merge ( $ annotationsBeforeParams , \array_values ( \array_filter ( $ paramsByName )), $ superfluousParams , $ annotationsAfterParams );
138134 }
139135}
0 commit comments