Server : nginx/1.18.0 System : Linux localhost 6.14.3-x86_64-linode168 #1 SMP PREEMPT_DYNAMIC Mon Apr 21 19:47:55 EDT 2025 x86_64 User : www-data ( 33) PHP Version : 8.0.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /var/www/ecommerce/vendor/friendsofphp/php-cs-fixer/src/Fixer/Phpdoc/ |
<?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz RumiĆski <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Fixer\Phpdoc;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\DocBlock\DocBlock;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
/**
* @author Graham Campbell <hello@gjcampbell.co.uk>
*/
final class PhpdocOrderFixer extends AbstractFixer
{
/**
* {@inheritdoc}
*/
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_DOC_COMMENT);
}
/**
* {@inheritdoc}
*/
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Annotations in PHPDoc should be ordered so that `@param` annotations come first, then `@throws` annotations, then `@return` annotations.',
[
new CodeSample(
'<?php
/**
* Hello there!
*
* @throws Exception|RuntimeException foo
* @custom Test!
* @return int Return the number of changes.
* @param string $foo
* @param bool $bar Bar
*/
'
),
]
);
}
/**
* {@inheritdoc}
*
* Must run before PhpdocAlignFixer, PhpdocSeparationFixer, PhpdocTrimFixer.
* Must run after AlignMultilineCommentFixer, CommentToPhpdocFixer, PhpdocAddMissingParamAnnotationFixer, PhpdocIndentFixer, PhpdocNoEmptyReturnFixer, PhpdocScalarFixer, PhpdocToCommentFixer, PhpdocTypesFixer.
*/
public function getPriority(): int
{
return -2;
}
/**
* {@inheritdoc}
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_DOC_COMMENT)) {
continue;
}
$content = $token->getContent();
// move param to start, return to end, leave throws in the middle
$content = $this->moveParamAnnotations($content);
// we're parsing the content again to make sure the internal
// state of the docblock is correct after the modifications
$content = $this->moveReturnAnnotations($content);
// persist the content at the end
$tokens[$index] = new Token([T_DOC_COMMENT, $content]);
}
}
/**
* Move all param annotations in before throws and return annotations.
*/
private function moveParamAnnotations(string $content): string
{
$doc = new DocBlock($content);
$params = $doc->getAnnotationsOfType('param');
// nothing to do if there are no param annotations
if (0 === \count($params)) {
return $content;
}
$others = $doc->getAnnotationsOfType(['throws', 'return']);
if (0 === \count($others)) {
return $content;
}
// get the index of the final line of the final param annotation
$end = end($params)->getEnd();
$line = $doc->getLine($end);
// move stuff about if required
foreach ($others as $other) {
if ($other->getStart() < $end) {
// we're doing this to maintain the original line indices
$line->setContent($line->getContent().$other->getContent());
$other->remove();
}
}
return $doc->getContent();
}
/**
* Move all return annotations after param and throws annotations.
*/
private function moveReturnAnnotations(string $content): string
{
$doc = new DocBlock($content);
$returns = $doc->getAnnotationsOfType('return');
// nothing to do if there are no return annotations
if (0 === \count($returns)) {
return $content;
}
$others = $doc->getAnnotationsOfType(['param', 'throws']);
// nothing to do if there are no other annotations
if (0 === \count($others)) {
return $content;
}
// get the index of the first line of the first return annotation
$start = $returns[0]->getStart();
$line = $doc->getLine($start);
// move stuff about if required
foreach (array_reverse($others) as $other) {
if ($other->getEnd() > $start) {
// we're doing this to maintain the original line indices
$line->setContent($other->getContent().$line->getContent());
$other->remove();
}
}
return $doc->getContent();
}
}