| Total Complexity | 62 |
| Total Lines | 230 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FqcnMethodSniff often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FqcnMethodSniff, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class FqcnMethodSniff implements CodeElementSniffInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @inheritDoc |
||
| 26 | */ |
||
| 27 | public function register(): array |
||
| 28 | { |
||
| 29 | return [ |
||
| 30 | ClassMethodElement::class, |
||
| 31 | // TraitMethodElement::class, // can be used to implement interface, not possible to know if it is extended |
||
| 32 | InterfaceMethodElement::class, |
||
| 33 | ]; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @inheritDoc |
||
| 38 | * @param AbstractFqcnMethodElement $method |
||
| 39 | */ |
||
| 40 | public function process(File $file, CodeElementInterface $method): void |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | protected function processMethod(File $file, AbstractFqcnMethodElement $method): void |
||
| 54 | { |
||
| 55 | $fnSig = $method->getSignature(); |
||
| 56 | $docBlock = $method->getDocBlock(); |
||
| 57 | $isMagicMethod = '__' === substr($fnSig->getName(), 0, 2); |
||
| 58 | $isConstructMethod = '__construct' === $fnSig->getName(); |
||
| 59 | $hasInheritDocTag = $docBlock->hasTag('inheritdoc'); |
||
| 60 | |||
| 61 | // @inheritDoc |
||
| 62 | // __construct can be detected as extended and magic, but we want to inspect it anyway |
||
| 63 | if (!$isConstructMethod) { |
||
| 64 | if ($hasInheritDocTag || $isMagicMethod) { |
||
| 65 | return; |
||
| 66 | } elseif ($method->isExtended()) { |
||
| 67 | $file->addWarningOnLine('Missing @inheritDoc tag', $method->getLine(), 'FqcnMethodSniff'); |
||
| 68 | return; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | // @param |
||
| 73 | foreach ($fnSig->getParams() as $fnParam) { |
||
| 74 | $paramName = $fnParam->getName(); |
||
| 75 | $tag = $docBlock->getParamTag($paramName); |
||
| 76 | |||
| 77 | $subject = sprintf('parameter $%s', $paramName); |
||
| 78 | |||
| 79 | $fnType = $fnParam->getType(); |
||
| 80 | $fnTypeLine = $fnParam->getLine(); |
||
| 81 | $docType = $tag ? $tag->getType() : null; |
||
| 82 | $docTypeLine = $tag ? $tag->getLine() : $fnTypeLine; |
||
| 83 | |||
| 84 | $this->processSigType($file, $docBlock, $subject, $fnType, $fnTypeLine, $docType, $docTypeLine); |
||
| 85 | } |
||
| 86 | |||
| 87 | // @return |
||
| 88 | if (!$isConstructMethod) { |
||
| 89 | $docType = $docBlock->getReturnTag(); |
||
| 90 | $this->processSigType( |
||
| 91 | $file, |
||
| 92 | $docBlock, |
||
| 93 | 'return value', |
||
| 94 | $fnSig->getReturnType(), |
||
| 95 | $fnSig->getReturnLine(), |
||
| 96 | $docType ? $docType->getType() : null, |
||
| 97 | $docType ? $docType->getLine() : $fnSig->getLine() |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 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 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | protected function containsType(TypeInterface $type, string $typeClassName): bool |
||
| 190 | } |
||
| 191 | |||
| 192 | protected function hasUselessDocBlock(AbstractFqcnMethodElement $method): bool |
||
| 252 | } |
||
| 253 | } |
||
| 254 |