Conditions | 25 |
Paths | 38 |
Total Lines | 80 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
102 | protected function processSigType( |
||
103 | File $file, |
||
104 | DocBlock $docBlock, |
||
105 | string $subject, |
||
106 | TypeInterface $fnType, |
||
107 | int $fnTypeLine, |
||
108 | ?TypeInterface $docType, |
||
109 | int $docTypeLine |
||
110 | ): void { |
||
111 | // @TODO Required mixed[][] instead of array[] |
||
112 | |||
113 | $warnings = []; |
||
114 | if ($docBlock instanceof UndefinedDocBlock) { |
||
115 | // doc_block:undefined, fn_type:? |
||
116 | if ($fnType instanceof UndefinedType) { |
||
117 | $warnings[$fnTypeLine] = 'Add type declaration for :subject: or create PHPDoc with type hint'; |
||
118 | } elseif ($this->containsType($fnType, ArrayType::class)) { |
||
119 | $warnings[$fnTypeLine] = 'Create PHPDoc with typed array type hint for :subject:, .e.g.: "string[]" or "SomeClass[]"'; |
||
120 | } |
||
121 | } elseif (null === $docType) { |
||
122 | // doc_block:defined, doc_tag:missing |
||
123 | if ('return value' === $subject) { // @TODO ?? |
||
124 | if (!($fnType instanceof VoidType)) { |
||
125 | $warnings[$fnTypeLine] = 'Missing PHPDoc tag or void type declaration for :subject:'; |
||
126 | } |
||
127 | } else { |
||
128 | $warnings[$fnTypeLine] = 'Missing PHPDoc tag for :subject:'; |
||
129 | } |
||
130 | } elseif ($docType instanceof UndefinedType) { |
||
131 | // doc_block:defined, doc_type:undefined |
||
132 | $suggestedFnType = TypeConverter::toExpectedDocType($fnType); |
||
133 | if (null !== $suggestedFnType) { |
||
134 | $warnings[$docTypeLine] = sprintf( |
||
135 | 'Add type hint in PHPDoc tag for :subject:, e.g. "%s"', |
||
136 | $suggestedFnType->toString() |
||
137 | ); |
||
138 | } else { |
||
139 | $warnings[$docTypeLine] = 'Add type hint in PHPDoc tag for :subject:'; |
||
140 | } |
||
141 | } elseif ($fnType instanceof UndefinedType) { |
||
142 | // doc_block:defined, doc_type:defined, fn_type:undefined |
||
143 | if ($docType instanceof NullType) { |
||
144 | $warnings[$fnTypeLine] = sprintf('Add type declaration for :subject:'); |
||
145 | } elseif ($suggestedFnType = TypeConverter::toFunctionType($docType)) { |
||
146 | $warnings[$fnTypeLine] = sprintf('Add type declaration for :subject:, e.g.: "%s"', $suggestedFnType->toString()); |
||
147 | } |
||
148 | } elseif ($this->containsType($fnType, ArrayType::class)) { |
||
149 | // doc_block:defined, doc_type:defined, fn_type:array |
||
150 | $docHasTypedArray = $this->containsType($docType, TypedArrayType::class); |
||
151 | $docHasArray = $this->containsType($docType, ArrayType::class); |
||
152 | |||
153 | if ($docHasTypedArray && $docHasArray) { |
||
154 | $warnings[$docTypeLine] = 'Remove array type, typed array type is present in PHPDoc for :subject:.'; |
||
155 | } elseif (!$docHasTypedArray && $docHasArray) { |
||
156 | $warnings[$docTypeLine] = 'Replace array type with typed array type in PHPDoc for :subject:. Use mixed[] for generic arrays.'; |
||
157 | } elseif (!$docHasTypedArray && !$docHasArray) { |
||
158 | $warnings[$docTypeLine] = 'Add typed array type in PHPDoc for :subject:. Use mixed[] for generic arrays.'; |
||
159 | } |
||
160 | } elseif ($fnType instanceof NullableType) { |
||
161 | // doc_block:defined, doc_type:defined, fn_type:nullable |
||
162 | if (!$this->containsType($docType, NullType::class)) { |
||
163 | $warnings[$docTypeLine] = 'Add "null" type hint in PHPDoc for :subject:'; |
||
164 | } |
||
165 | } else { |
||
166 | // doc_block:defined, doc_type:defined, fn_type:defined |
||
167 | $expectedDocType = TypeConverter::toExpectedDocType($fnType); |
||
168 | $expectedDocTypes = $expectedDocType instanceof CompoundType |
||
169 | ? $expectedDocType->getTypes() |
||
170 | : array_filter([$expectedDocType]); |
||
171 | |||
172 | foreach ($expectedDocTypes as $expectedDocType) { |
||
173 | if (!$this->containsType($docType, get_class($expectedDocType))) { |
||
174 | $warnings[$docTypeLine] = sprintf('Add "%s" type hint in PHPDoc for :subject:', $fnType->toString()); |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | |||
179 | foreach ($warnings as $line => $warningTpl) { |
||
180 | $warning = str_replace(':subject:', $subject, $warningTpl); |
||
181 | $file->addWarningOnLine($warning, $line, 'FqcnMethodSniff'); |
||
182 | } |
||
254 |