* Dariusz Rumiński * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Phpdoc; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer */ final class PhpdocAlignFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases * * @param _AutogeneratedInputConfiguration $configuration */ public function testFix( array $configuration, string $expected, ?string $input = null, ?WhitespacesFixerConfig $whitespacesFixerConfig = null ): void { $this->fixer->configure($configuration); if (null !== $whitespacesFixerConfig) { $this->fixer->setWhitespacesConfig($whitespacesFixerConfig); } $this->doTest($expected, $input); } public static function provideFixCases(): iterable { yield 'none, one and four spaces between type and variable' => [ ['tags' => ['param']], ' [ ['tags' => ['param']], ' [ ['tags' => ['param'], 'align' => PhpdocAlignFixer::ALIGN_LEFT], ' [ ['tags' => ['param']], ' [ ['tags' => ['param'], 'align' => PhpdocAlignFixer::ALIGN_LEFT], ' [ ['tags' => ['param', 'property', 'method']], ' [ ['tags' => ['param', 'property', 'method'], 'align' => PhpdocAlignFixer::ALIGN_LEFT], ' [ ['tags' => ['param', 'return', 'throws']], ' [ ['tags' => ['param', 'return', 'throws'], 'align' => PhpdocAlignFixer::ALIGN_LEFT], ' [ ['tags' => ['param', 'throws', 'return']], ' [ ['tags' => ['param', 'return']], ' [ ['tags' => ['return']], ' [ ['tags' => ['param', 'return']], ' [ ['tags' => ['return']], ' [ ['tags' => ['var']], '|Value[] * @ORM\ManyToMany( * targetEntity="\Dl\Component\DomainModel\Product\Value\AbstractValue", * inversedBy="externalAliases" * ) */ private $values; } ', ]; yield 'left align 2' => [ ['tags' => ['param'], 'align' => PhpdocAlignFixer::ALIGN_LEFT], ' [ ['tags' => ['param']], ' [ ['tags' => ['var']], ' [ ['tags' => ['type']], ' [ ['tags' => ['var']], ' [ ['tags' => ['var']], ' [ ['tags' => ['type']], ' [ ['tags' => ['param']], " [ ['tags' => ['return']], ' [ ['tags' => ['param', 'return']], ' [ ['tags' => ['param', 'return'], 'align' => PhpdocAlignFixer::ALIGN_LEFT], ' [ ['tags' => ['type']], " [ ['tags' => ['param', 'return']], " [ [], " [ [], " [ [], " [ ['tags' => ['var']], " [ ['tags' => ['param', 'return']], ' [ [], ' [ ['tags' => ['param', 'property', 'return', 'throws', 'type', 'var']], ' [ [], ' [ ['tags' => ['param', 'method', 'return', 'throws', 'type', 'var']], ' [ ['tags' => ['method', 'property']], ' [ ['tags' => ['method', 'property'], 'align' => PhpdocAlignFixer::ALIGN_LEFT], ' [ ['tags' => ['method']], ' [ ['tags' => ['method', 'property']], ' [ ['tags' => ['method']], ' [ ['tags' => ['method', 'property']], ' [ ['tags' => ['method', 'property'], 'align' => PhpdocAlignFixer::ALIGN_LEFT], ' [ ['tags' => ['param', 'return', 'throws']], ' [ ['tags' => ['param', 'return', 'throws'], 'align' => PhpdocAlignFixer::ALIGN_LEFT], ' [ ['tags' => []], ' [ ['tags' => ['param']], ' [ ['tags' => ['param']], ' [ ['tags' => ['param'], 'align' => PhpdocAlignFixer::ALIGN_LEFT], ' [ ['tags' => ['property', 'property-read', 'property-write']], ' [ ['tags' => ['param', 'return', 'throws', 'type', 'var']], ' [ ['tags' => ['param', 'return', 'throws', 'type', 'var', 'method']], ' [ ['tags' => ['param', 'return', 'throws', 'type', 'var']], ' [ [], ' [ [], ' $value */ /** * @param array $arrayOfIntegers * @param array $arrayOfStrings */ ', ]; yield 'closure types containing backslash' => [ [], '> $data */ /** * @param string $input * @param \Closure $fn * @param \Closure(bool):int $fn2 * @param Closure $fn3 * @param Closure(string):string $fn4 * @param array> $data */ /** * @var string $value * @var \Closure(string): string $callback * @var Closure(int): bool $callback2 */ /** * @param string $value * @param \Closure(string): string $callback * @param Closure(int): bool $callback2 */ /** * @var Closure(array): bool $callback1 * @var \Closure(string): string $callback2 */ /** * @param Closure(array): bool $callback1 * @param \Closure(string): string $callback2 */ ', ]; yield 'types parenthesized' => [ [], ' $allowedTypes * @param null|list<\Closure(mixed): (bool|null|scalar)> $allowedValues */ ', ' $allowedTypes * @param null|list<\Closure(mixed): (bool|null|scalar)> $allowedValues */ ', ]; yield 'callable types with ugly code 1' => [ [], ' [ [], ' [ ['tags' => ['param', 'xxx-xxxxxxxxx']], ' [ [], ' [ [], ' [ [ 'align' => PhpdocAlignFixer::ALIGN_LEFT, 'spacing' => ['param' => 2], ], ' [ [ 'align' => PhpdocAlignFixer::ALIGN_VERTICAL, 'spacing' => ['param' => 2, 'return' => 4], ], ' [ [ 'align' => PhpdocAlignFixer::ALIGN_LEFT, 'spacing' => ['param' => 2, 'return' => 4], ], ' [ [ 'align' => PhpdocAlignFixer::ALIGN_LEFT, 'spacing' => ['_default' => 2, 'return' => 4], ], 'expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessage($expectedMessage); $this->fixer->configure($config); } /** * @return iterable, string}> */ public static function provideInvalidConfigurationCases(): iterable { yield 'zero' => [ ['spacing' => 0], 'The option "spacing" is invalid. All spacings must be greater than zero.', ]; yield 'negative' => [ ['spacing' => -2], 'The option "spacing" is invalid. All spacings must be greater than zero.', ]; yield 'zeroInArray' => [ ['spacing' => ['param' => 1, 'return' => 0]], 'The option "spacing" is invalid. All spacings must be greater than zero.', ]; yield 'negativeInArray' => [ [ 'align' => PhpdocAlignFixer::ALIGN_LEFT, 'spacing' => ['return' => 2, 'param' => -1], ], 'The option "spacing" is invalid. All spacings must be greater than zero.', ]; } }