|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * MIT License |
| 5 | + * For full license information, please view the LICENSE file that was distributed with this source code. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace PhpCollective\Sniffs\Commenting; |
| 9 | + |
| 10 | +use PHP_CodeSniffer\Files\File; |
| 11 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 12 | +use PhpCollective\Traits\CommentingTrait; |
| 13 | +use PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode; |
| 14 | +use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; |
| 15 | +use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode; |
| 16 | +use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; |
| 17 | +use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; |
| 18 | +use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
| 19 | +use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode; |
| 20 | +use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
| 21 | +use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; |
| 22 | +use PHPStan\PhpDocParser\Printer\Printer; |
| 23 | + |
| 24 | +/** |
| 25 | + * Disallows use of `?type` in favor of `type|null`. Reduces conflict or issues with other sniffs. |
| 26 | + */ |
| 27 | +class DisallowShorthandNullableTypeHintSniff implements Sniff |
| 28 | +{ |
| 29 | + use CommentingTrait; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + public const CODE_DISALLOWED_SHORTHAND_TYPE_HINT = 'DisallowedShorthandTypeHint'; |
| 35 | + |
| 36 | + /** |
| 37 | + * @inheritDoc |
| 38 | + */ |
| 39 | + public function register(): array |
| 40 | + { |
| 41 | + return [ |
| 42 | + T_DOC_COMMENT_STRING, |
| 43 | + ]; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @inheritDoc |
| 48 | + */ |
| 49 | + public function process(File $phpcsFile, $pointer): void |
| 50 | + { |
| 51 | + $tokens = $phpcsFile->getTokens(); |
| 52 | + $docCommentContent = $tokens[$pointer]['content']; |
| 53 | + |
| 54 | + /** @var \PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode|\PHPStan\PhpDocParser\Ast\PhpDoc\TypelessParamTagValueNode|\PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode|\PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode|\PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode $valueNode */ |
| 55 | + $valueNode = static::getValueNode($tokens[$pointer - 2]['content'], $docCommentContent); |
| 56 | + |
| 57 | + $printer = new Printer(); |
| 58 | + $before = $printer->print($valueNode); |
| 59 | + |
| 60 | + // Check if the value node is invalid and handle it |
| 61 | + if ($valueNode instanceof InvalidTagValueNode) { |
| 62 | + // Attempt to clean up and process invalid types |
| 63 | + $fixedNode = $this->fixInvalidTagValueNode($valueNode); |
| 64 | + if ($fixedNode) { |
| 65 | + $valueNode = $fixedNode; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if ($valueNode instanceof InvalidTagValueNode) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // Traverse and fix the nullable types |
| 74 | + $this->traversePhpDocNode($valueNode); |
| 75 | + |
| 76 | + $after = $printer->print($valueNode); |
| 77 | + |
| 78 | + if ($after === $before) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + $message = sprintf('Shorthand nullable `%s` invalid, use `%s` instead.', $before, $after); |
| 83 | + $fixable = $phpcsFile->addFixableError($message, $pointer, static::CODE_DISALLOWED_SHORTHAND_TYPE_HINT); |
| 84 | + if ($fixable) { |
| 85 | + $phpcsFile->fixer->beginChangeset(); |
| 86 | + $phpcsFile->fixer->replaceToken($pointer, $after); |
| 87 | + $phpcsFile->fixer->endChangeset(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Attempt to fix an InvalidTagValueNode by parsing and correcting the types manually. |
| 93 | + * |
| 94 | + * @param \PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode $invalidNode |
| 95 | + * |
| 96 | + * @return \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode|null |
| 97 | + */ |
| 98 | + protected function fixInvalidTagValueNode(InvalidTagValueNode $invalidNode): ?PhpDocTagValueNode |
| 99 | + { |
| 100 | + $value = $invalidNode->value; |
| 101 | + $rest = ''; |
| 102 | + if (str_contains($value, '$')) { |
| 103 | + $string = trim(substr($value, 0, (int)strpos($value, '$'))); |
| 104 | + $rest = trim(substr($value, strlen($string))); |
| 105 | + $value = $string; |
| 106 | + } |
| 107 | + |
| 108 | + // Try to parse and correct the invalid node's type (e.g., `?string|null`) |
| 109 | + if (str_contains($value, '|')) { |
| 110 | + // Split the types |
| 111 | + $types = explode('|', $value); |
| 112 | + |
| 113 | + $transformedTypes = []; |
| 114 | + $hasNullable = false; |
| 115 | + |
| 116 | + foreach ($types as $type) { |
| 117 | + $type = trim($type); |
| 118 | + |
| 119 | + // Handle `?Type` shorthand |
| 120 | + if (str_starts_with($type, '?')) { |
| 121 | + $type = substr($type, 1); // Remove leading '?' |
| 122 | + $transformedTypes[] = new IdentifierTypeNode($type); |
| 123 | + $hasNullable = true; // Mark as nullable |
| 124 | + } elseif (strtolower($type) === 'null') { |
| 125 | + // If 'null' is encountered, mark as nullable but don't add now |
| 126 | + $hasNullable = true; |
| 127 | + } else { |
| 128 | + $transformedTypes[] = new IdentifierTypeNode($type); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Add `null` at the end if the type is nullable |
| 133 | + if ($hasNullable) { |
| 134 | + $transformedTypes[] = new IdentifierTypeNode('null'); |
| 135 | + } |
| 136 | + |
| 137 | + // Create a new UnionTypeNode with the transformed types |
| 138 | + return new ParamTagValueNode( |
| 139 | + new UnionTypeNode($transformedTypes), |
| 140 | + false, |
| 141 | + $rest, |
| 142 | + '', |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Traverse and transform the PHPDoc AST. |
| 151 | + * |
| 152 | + * @param \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode $phpDocNode |
| 153 | + * |
| 154 | + * @return void |
| 155 | + */ |
| 156 | + protected function traversePhpDocNode(PhpDocTagValueNode $phpDocNode): void |
| 157 | + { |
| 158 | + if ( |
| 159 | + $phpDocNode instanceof ParamTagValueNode |
| 160 | + || $phpDocNode instanceof ReturnTagValueNode |
| 161 | + || $phpDocNode instanceof VarTagValueNode |
| 162 | + ) { |
| 163 | + echo PHP_EOL . 'processing...' . PHP_EOL; |
| 164 | + $phpDocNode->type = $this->transformNullableType($phpDocNode->type); |
| 165 | + } |
| 166 | + |
| 167 | + echo PHP_EOL . PHP_EOL; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Traverse and transform nullable types. |
| 172 | + * |
| 173 | + * @param \PHPStan\PhpDocParser\Ast\Type\TypeNode $typeNode |
| 174 | + * |
| 175 | + * @return \PHPStan\PhpDocParser\Ast\Type\TypeNode |
| 176 | + */ |
| 177 | + protected function transformNullableType(TypeNode $typeNode): TypeNode |
| 178 | + { |
| 179 | + if ($typeNode instanceof NullableTypeNode) { |
| 180 | + $innerType = $typeNode->type; |
| 181 | + |
| 182 | + // Convert `?Type` to `Type|null` |
| 183 | + return new UnionTypeNode([ |
| 184 | + $innerType, |
| 185 | + new IdentifierTypeNode('null'), |
| 186 | + ]); |
| 187 | + } |
| 188 | + |
| 189 | + // Handle UnionTypeNode (e.g., `Type|null`) |
| 190 | + if ($typeNode instanceof UnionTypeNode) { |
| 191 | + $transformedTypes = []; |
| 192 | + foreach ($typeNode->types as $subType) { |
| 193 | + $transformedTypes[] = $this->transformNullableType($subType); // Recursively transform |
| 194 | + } |
| 195 | + |
| 196 | + return new UnionTypeNode($transformedTypes); |
| 197 | + } |
| 198 | + |
| 199 | + return $typeNode; |
| 200 | + } |
| 201 | +} |
0 commit comments