|
| 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\TypelessParamTagValueNode; |
| 18 | +use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; |
| 19 | +use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
| 20 | +use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode; |
| 21 | +use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
| 22 | +use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; |
| 23 | +use PHPStan\PhpDocParser\Printer\Printer; |
| 24 | + |
| 25 | +/** |
| 26 | + * Disallows use of `?type` in favor of `type|null`. Reduces conflict or issues with other sniffs. |
| 27 | + */ |
| 28 | +class DisallowShorthandNullableTypeHintSniff implements Sniff |
| 29 | +{ |
| 30 | + use CommentingTrait; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + public const CODE_DISALLOWED_SHORTHAND_TYPE_HINT = 'DisallowedShorthandTypeHint'; |
| 36 | + |
| 37 | + /** |
| 38 | + * @inheritDoc |
| 39 | + */ |
| 40 | + public function register(): array |
| 41 | + { |
| 42 | + return [ |
| 43 | + T_DOC_COMMENT_STRING, |
| 44 | + ]; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @inheritDoc |
| 49 | + */ |
| 50 | + public function process(File $phpcsFile, $pointer): void |
| 51 | + { |
| 52 | + $tokens = $phpcsFile->getTokens(); |
| 53 | + $docCommentContent = $tokens[$pointer]['content']; |
| 54 | + |
| 55 | + /** @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\PropertyTagValueNode $valueNode */ |
| 56 | + $valueNode = static::getValueNode($tokens[$pointer - 2]['content'], $docCommentContent); |
| 57 | + if ($valueNode instanceof InvalidTagValueNode || $valueNode instanceof TypelessParamTagValueNode) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + $printer = new Printer(); |
| 62 | + $before = $printer->print($valueNode); |
| 63 | + // Traverse and fix the nullable types |
| 64 | + $this->traversePhpDocNode($valueNode); |
| 65 | + |
| 66 | + $after = $printer->print($valueNode); |
| 67 | + |
| 68 | + if ($after === $before) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + $message = sprintf('Shorthand nullable `%s` invalid, use `%s` instead.', $before, $after); |
| 73 | + $fixable = $phpcsFile->addFixableError($message, $pointer, static::CODE_DISALLOWED_SHORTHAND_TYPE_HINT); |
| 74 | + if ($fixable) { |
| 75 | + $phpcsFile->fixer->beginChangeset(); |
| 76 | + $phpcsFile->fixer->replaceToken($pointer, $after); |
| 77 | + $phpcsFile->fixer->endChangeset(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Traverse and transform the PHPDoc AST. |
| 83 | + * |
| 84 | + * @param \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode $phpDocNode |
| 85 | + * |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + protected function traversePhpDocNode(PhpDocTagValueNode $phpDocNode): void |
| 89 | + { |
| 90 | + if ( |
| 91 | + $phpDocNode instanceof ParamTagValueNode |
| 92 | + || $phpDocNode instanceof ReturnTagValueNode |
| 93 | + || $phpDocNode instanceof VarTagValueNode |
| 94 | + ) { |
| 95 | + // Traverse the type node recursively |
| 96 | + $phpDocNode->type = $this->transformNullableType($phpDocNode->type); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Traverse and transform nullable types. |
| 102 | + * |
| 103 | + * @param \PHPStan\PhpDocParser\Ast\Type\TypeNode $typeNode |
| 104 | + * |
| 105 | + * @return \PHPStan\PhpDocParser\Ast\Type\TypeNode |
| 106 | + */ |
| 107 | + protected function transformNullableType(TypeNode $typeNode): TypeNode |
| 108 | + { |
| 109 | + if ($typeNode instanceof NullableTypeNode) { |
| 110 | + $innerType = $typeNode->type; |
| 111 | + |
| 112 | + // Convert `?Type` to `Type|null` |
| 113 | + return new UnionTypeNode([ |
| 114 | + $innerType, |
| 115 | + new IdentifierTypeNode('null'), |
| 116 | + ]); |
| 117 | + } |
| 118 | + |
| 119 | + // Recursively handle UnionTypeNode (e.g., `Type|null`) |
| 120 | + if ($typeNode instanceof UnionTypeNode) { |
| 121 | + // Traverse each type in the union and transform nullable types |
| 122 | + foreach ($typeNode->types as &$subType) { |
| 123 | + $subType = $this->transformNullableType($subType); |
| 124 | + } |
| 125 | + |
| 126 | + return $typeNode; |
| 127 | + } |
| 128 | + |
| 129 | + // Recursively handle other nodes that might contain nested types |
| 130 | + if (property_exists($typeNode, 'types') && is_array($typeNode->types)) { |
| 131 | + foreach ($typeNode->types as &$subType) { |
| 132 | + $subType = $this->transformNullableType($subType); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return $typeNode; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @param array<string> $types |
| 141 | + * |
| 142 | + * @return bool |
| 143 | + */ |
| 144 | + protected function containsShorthand(array $types): bool |
| 145 | + { |
| 146 | + foreach ($types as $type) { |
| 147 | + if (str_starts_with($type, '?')) { |
| 148 | + return true; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return false; |
| 153 | + } |
| 154 | +} |
0 commit comments