<?php
namespace App\Fixers;
use PhpCsFixer\DocBlock\TypeExpression;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;
/*
* Some code in this file is part of PHP CS Fixer.
*
* Copyright (c) 2012-2022 Fabien Potencier, Dariusz Rumiński
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class LaravelPhpdocAlignmentFixer implements FixerInterface
{
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'Laravel/laravel_phpdoc_alignment';
}
/**
* {@inheritdoc}
*/
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([T_DOC_COMMENT]);
}
/**
* {@inheritdoc}
*/
public function isRisky(): bool
{
return false;
}
/**
* {@inheritdoc}
*/
public function fix(SplFileInfo $file, Tokens $tokens): void
{
for ($index = $tokens->count() - 1; $index > 0; $index--) {
if (! $tokens[$index]->isGivenKind([\T_DOC_COMMENT])) {
continue;
}
$newContent = preg_replace_callback(
'/(?P<tag>@param)\s+(?P<hint>(?:'.TypeExpression::REGEX_TYPES.')?)\s+(?P<var>(?:&|\.{3})?\$\S+)/ux',
fn ($matches) => $matches['tag'].' '.$matches['hint'].' '.$matches['var'],
$tokens[$index]->getContent()
);
if ($newContent == $tokens[$index]->getContent()) {
continue;
}
$tokens[$index] = new Token([T_DOC_COMMENT, $newContent]);
}
}
/**
* {@inheritdoc}
*/
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition('@param and type definition must be followed by two spaces.', []);
}
/**
* {@inheritdoc}
*/
public function getPriority(): int
{
return -42;
}
/**
* {@inheritdoc}
*/
public function supports(SplFileInfo $file): bool
{
return true;
}
}