Missing dependancies

This commit is contained in:
tokslaw7
2023-06-21 12:19:22 +00:00
parent fbf3f180c2
commit 421f25c80d
2356 changed files with 342670 additions and 4743 deletions
@@ -0,0 +1,108 @@
<?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\DoctrineAnnotation;
use Doctrine\Common\Annotations\DocLexer;
use PhpCsFixer\AbstractDoctrineAnnotationFixer;
use PhpCsFixer\Doctrine\Annotation\Tokens;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
/**
* Forces the configured operator for assignment in arrays in Doctrine Annotations.
*/
final class DoctrineAnnotationArrayAssignmentFixer extends AbstractDoctrineAnnotationFixer
{
/**
* {@inheritdoc}
*/
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Doctrine annotations must use configured operator for assignment in arrays.',
[
new CodeSample(
"<?php\n/**\n * @Foo({bar : \"baz\"})\n */\nclass Bar {}\n"
),
new CodeSample(
"<?php\n/**\n * @Foo({bar = \"baz\"})\n */\nclass Bar {}\n",
['operator' => ':']
),
]
);
}
/**
* {@inheritdoc}
*
* Must run before DoctrineAnnotationSpacesFixer.
*/
public function getPriority(): int
{
return 1;
}
/**
* {@inheritdoc}
*/
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
$options = parent::createConfigurationDefinition()->getOptions();
$operator = new FixerOptionBuilder('operator', 'The operator to use.');
$options[] = $operator
->setAllowedValues(['=', ':'])
->setDefault('=')
->getOption()
;
return new FixerConfigurationResolver($options);
}
/**
* {@inheritdoc}
*/
protected function fixAnnotations(Tokens $doctrineAnnotationTokens): void
{
$scopes = [];
foreach ($doctrineAnnotationTokens as $token) {
if ($token->isType(DocLexer::T_OPEN_PARENTHESIS)) {
$scopes[] = 'annotation';
continue;
}
if ($token->isType(DocLexer::T_OPEN_CURLY_BRACES)) {
$scopes[] = 'array';
continue;
}
if ($token->isType([DocLexer::T_CLOSE_PARENTHESIS, DocLexer::T_CLOSE_CURLY_BRACES])) {
array_pop($scopes);
continue;
}
if ('array' === end($scopes) && $token->isType([DocLexer::T_EQUALS, DocLexer::T_COLON])) {
$token->setContent($this->configuration['operator']);
}
}
}
}
@@ -0,0 +1,127 @@
<?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\DoctrineAnnotation;
use Doctrine\Common\Annotations\DocLexer;
use PhpCsFixer\AbstractDoctrineAnnotationFixer;
use PhpCsFixer\Doctrine\Annotation\Token;
use PhpCsFixer\Doctrine\Annotation\Tokens;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
/**
* Adds braces to Doctrine annotations when missing.
*/
final class DoctrineAnnotationBracesFixer extends AbstractDoctrineAnnotationFixer
{
/**
* {@inheritdoc}
*/
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Doctrine annotations without arguments must use the configured syntax.',
[
new CodeSample(
"<?php\n/**\n * @Foo()\n */\nclass Bar {}\n"
),
new CodeSample(
"<?php\n/**\n * @Foo\n */\nclass Bar {}\n",
['syntax' => 'with_braces']
),
]
);
}
/**
* {@inheritdoc}
*/
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver(array_merge(
parent::createConfigurationDefinition()->getOptions(),
[
(new FixerOptionBuilder('syntax', 'Whether to add or remove braces.'))
->setAllowedValues(['with_braces', 'without_braces'])
->setDefault('without_braces')
->getOption(),
]
));
}
/**
* {@inheritdoc}
*/
protected function fixAnnotations(Tokens $doctrineAnnotationTokens): void
{
if ('without_braces' === $this->configuration['syntax']) {
$this->removesBracesFromAnnotations($doctrineAnnotationTokens);
} else {
$this->addBracesToAnnotations($doctrineAnnotationTokens);
}
}
private function addBracesToAnnotations(Tokens $tokens): void
{
foreach ($tokens as $index => $token) {
if (!$tokens[$index]->isType(DocLexer::T_AT)) {
continue;
}
$braceIndex = $tokens->getNextMeaningfulToken($index + 1);
if (null !== $braceIndex && $tokens[$braceIndex]->isType(DocLexer::T_OPEN_PARENTHESIS)) {
continue;
}
$tokens->insertAt($index + 2, new Token(DocLexer::T_OPEN_PARENTHESIS, '('));
$tokens->insertAt($index + 3, new Token(DocLexer::T_CLOSE_PARENTHESIS, ')'));
}
}
private function removesBracesFromAnnotations(Tokens $tokens): void
{
for ($index = 0, $max = \count($tokens); $index < $max; ++$index) {
if (!$tokens[$index]->isType(DocLexer::T_AT)) {
continue;
}
$openBraceIndex = $tokens->getNextMeaningfulToken($index + 1);
if (null === $openBraceIndex) {
continue;
}
if (!$tokens[$openBraceIndex]->isType(DocLexer::T_OPEN_PARENTHESIS)) {
continue;
}
$closeBraceIndex = $tokens->getNextMeaningfulToken($openBraceIndex);
if (null === $closeBraceIndex) {
continue;
}
if (!$tokens[$closeBraceIndex]->isType(DocLexer::T_CLOSE_PARENTHESIS)) {
continue;
}
for ($currentIndex = $index + 2; $currentIndex <= $closeBraceIndex; ++$currentIndex) {
$tokens[$currentIndex]->clear();
}
}
}
}
@@ -0,0 +1,193 @@
<?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\DoctrineAnnotation;
use Doctrine\Common\Annotations\DocLexer;
use PhpCsFixer\AbstractDoctrineAnnotationFixer;
use PhpCsFixer\Doctrine\Annotation\Tokens;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Preg;
final class DoctrineAnnotationIndentationFixer extends AbstractDoctrineAnnotationFixer
{
/**
* {@inheritdoc}
*/
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Doctrine annotations must be indented with four spaces.',
[
new CodeSample("<?php\n/**\n * @Foo(\n * foo=\"foo\"\n * )\n */\nclass Bar {}\n"),
new CodeSample(
"<?php\n/**\n * @Foo({@Bar,\n * @Baz})\n */\nclass Bar {}\n",
['indent_mixed_lines' => true]
),
]
);
}
/**
* {@inheritdoc}
*/
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver(array_merge(
parent::createConfigurationDefinition()->getOptions(),
[
(new FixerOptionBuilder('indent_mixed_lines', 'Whether to indent lines that have content before closing parenthesis.'))
->setAllowedTypes(['bool'])
->setDefault(false)
->getOption(),
]
));
}
/**
* {@inheritdoc}
*/
protected function fixAnnotations(Tokens $doctrineAnnotationTokens): void
{
$annotationPositions = [];
for ($index = 0, $max = \count($doctrineAnnotationTokens); $index < $max; ++$index) {
if (!$doctrineAnnotationTokens[$index]->isType(DocLexer::T_AT)) {
continue;
}
$annotationEndIndex = $doctrineAnnotationTokens->getAnnotationEnd($index);
if (null === $annotationEndIndex) {
return;
}
$annotationPositions[] = [$index, $annotationEndIndex];
$index = $annotationEndIndex;
}
$indentLevel = 0;
foreach ($doctrineAnnotationTokens as $index => $token) {
if (!$token->isType(DocLexer::T_NONE) || !str_contains($token->getContent(), "\n")) {
continue;
}
if (!$this->indentationCanBeFixed($doctrineAnnotationTokens, $index, $annotationPositions)) {
continue;
}
$braces = $this->getLineBracesCount($doctrineAnnotationTokens, $index);
$delta = $braces[0] - $braces[1];
$mixedBraces = 0 === $delta && $braces[0] > 0;
$extraIndentLevel = 0;
if ($indentLevel > 0 && ($delta < 0 || $mixedBraces)) {
--$indentLevel;
if (true === $this->configuration['indent_mixed_lines'] && $this->isClosingLineWithMeaningfulContent($doctrineAnnotationTokens, $index)) {
$extraIndentLevel = 1;
}
}
$token->setContent(Preg::replace(
'/(\n( +\*)?) *$/',
'$1'.str_repeat(' ', 4 * ($indentLevel + $extraIndentLevel) + 1),
$token->getContent()
));
if ($delta > 0 || $mixedBraces) {
++$indentLevel;
}
}
}
/**
* @return int[]
*/
private function getLineBracesCount(Tokens $tokens, int $index): array
{
$opening = 0;
$closing = 0;
while (isset($tokens[++$index])) {
$token = $tokens[$index];
if ($token->isType(DocLexer::T_NONE) && str_contains($token->getContent(), "\n")) {
break;
}
if ($token->isType([DocLexer::T_OPEN_PARENTHESIS, DocLexer::T_OPEN_CURLY_BRACES])) {
++$opening;
continue;
}
if (!$token->isType([DocLexer::T_CLOSE_PARENTHESIS, DocLexer::T_CLOSE_CURLY_BRACES])) {
continue;
}
if ($opening > 0) {
--$opening;
} else {
++$closing;
}
}
return [$opening, $closing];
}
private function isClosingLineWithMeaningfulContent(Tokens $tokens, int $index): bool
{
while (isset($tokens[++$index])) {
$token = $tokens[$index];
if ($token->isType(DocLexer::T_NONE)) {
if (str_contains($token->getContent(), "\n")) {
return false;
}
continue;
}
return !$token->isType([DocLexer::T_CLOSE_PARENTHESIS, DocLexer::T_CLOSE_CURLY_BRACES]);
}
return false;
}
/**
* @param array<array<int>> $annotationPositions Pairs of begin and end indices of main annotations
*/
private function indentationCanBeFixed(Tokens $tokens, int $newLineTokenIndex, array $annotationPositions): bool
{
foreach ($annotationPositions as $position) {
if ($newLineTokenIndex >= $position[0] && $newLineTokenIndex <= $position[1]) {
return true;
}
}
for ($index = $newLineTokenIndex + 1, $max = \count($tokens); $index < $max; ++$index) {
$token = $tokens[$index];
if (str_contains($token->getContent(), "\n")) {
return false;
}
return $tokens[$index]->isType(DocLexer::T_AT);
}
return false;
}
}
@@ -0,0 +1,301 @@
<?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\DoctrineAnnotation;
use Doctrine\Common\Annotations\DocLexer;
use PhpCsFixer\AbstractDoctrineAnnotationFixer;
use PhpCsFixer\Doctrine\Annotation\Token;
use PhpCsFixer\Doctrine\Annotation\Tokens;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Preg;
/**
* Fixes spaces around commas and assignment operators in Doctrine annotations.
*/
final class DoctrineAnnotationSpacesFixer extends AbstractDoctrineAnnotationFixer
{
/**
* {@inheritdoc}
*/
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Fixes spaces in Doctrine annotations.',
[
new CodeSample(
"<?php\n/**\n * @Foo ( )\n */\nclass Bar {}\n\n/**\n * @Foo(\"bar\" ,\"baz\")\n */\nclass Bar2 {}\n\n/**\n * @Foo(foo = \"foo\", bar = {\"foo\":\"foo\", \"bar\"=\"bar\"})\n */\nclass Bar3 {}\n"
),
new CodeSample(
"<?php\n/**\n * @Foo(foo = \"foo\", bar = {\"foo\":\"foo\", \"bar\"=\"bar\"})\n */\nclass Bar {}\n",
['after_array_assignments_equals' => false, 'before_array_assignments_equals' => false]
),
],
'There must not be any space around parentheses; commas must be preceded by no space and followed by one space; there must be no space around named arguments assignment operator; there must be one space around array assignment operator.'
);
}
/**
* {@inheritdoc}
*
* Must run after DoctrineAnnotationArrayAssignmentFixer.
*/
public function getPriority(): int
{
return 0;
}
/**
* {@inheritdoc}
*/
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver(array_merge(
parent::createConfigurationDefinition()->getOptions(),
[
(new FixerOptionBuilder('around_parentheses', 'Whether to fix spaces around parentheses.'))
->setAllowedTypes(['bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('around_commas', 'Whether to fix spaces around commas.'))
->setAllowedTypes(['bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('before_argument_assignments', 'Whether to add, remove or ignore spaces before argument assignment operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(false)
->getOption(),
(new FixerOptionBuilder('after_argument_assignments', 'Whether to add, remove or ignore spaces after argument assignment operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(false)
->getOption(),
(new FixerOptionBuilder('before_array_assignments_equals', 'Whether to add, remove or ignore spaces before array `=` assignment operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('after_array_assignments_equals', 'Whether to add, remove or ignore spaces after array assignment `=` operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('before_array_assignments_colon', 'Whether to add, remove or ignore spaces before array `:` assignment operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('after_array_assignments_colon', 'Whether to add, remove or ignore spaces after array assignment `:` operator.'))
->setAllowedTypes(['null', 'bool'])
->setDefault(true)
->getOption(),
]
));
}
/**
* {@inheritdoc}
*/
protected function fixAnnotations(Tokens $doctrineAnnotationTokens): void
{
if (true === $this->configuration['around_parentheses']) {
$this->fixSpacesAroundParentheses($doctrineAnnotationTokens);
}
if (true === $this->configuration['around_commas']) {
$this->fixSpacesAroundCommas($doctrineAnnotationTokens);
}
if (
null !== $this->configuration['before_argument_assignments']
|| null !== $this->configuration['after_argument_assignments']
|| null !== $this->configuration['before_array_assignments_equals']
|| null !== $this->configuration['after_array_assignments_equals']
|| null !== $this->configuration['before_array_assignments_colon']
|| null !== $this->configuration['after_array_assignments_colon']
) {
$this->fixAroundAssignments($doctrineAnnotationTokens);
}
}
private function fixSpacesAroundParentheses(Tokens $tokens): void
{
$inAnnotationUntilIndex = null;
foreach ($tokens as $index => $token) {
if (null !== $inAnnotationUntilIndex) {
if ($index === $inAnnotationUntilIndex) {
$inAnnotationUntilIndex = null;
continue;
}
} elseif ($tokens[$index]->isType(DocLexer::T_AT)) {
$endIndex = $tokens->getAnnotationEnd($index);
if (null !== $endIndex) {
$inAnnotationUntilIndex = $endIndex + 1;
}
continue;
}
if (null === $inAnnotationUntilIndex) {
continue;
}
if (!$token->isType([DocLexer::T_OPEN_PARENTHESIS, DocLexer::T_CLOSE_PARENTHESIS])) {
continue;
}
if ($token->isType(DocLexer::T_OPEN_PARENTHESIS)) {
$token = $tokens[$index - 1];
if ($token->isType(DocLexer::T_NONE)) {
$token->clear();
}
$token = $tokens[$index + 1];
} else {
$token = $tokens[$index - 1];
}
if ($token->isType(DocLexer::T_NONE)) {
if (str_contains($token->getContent(), "\n")) {
continue;
}
$token->clear();
}
}
}
private function fixSpacesAroundCommas(Tokens $tokens): void
{
$inAnnotationUntilIndex = null;
foreach ($tokens as $index => $token) {
if (null !== $inAnnotationUntilIndex) {
if ($index === $inAnnotationUntilIndex) {
$inAnnotationUntilIndex = null;
continue;
}
} elseif ($tokens[$index]->isType(DocLexer::T_AT)) {
$endIndex = $tokens->getAnnotationEnd($index);
if (null !== $endIndex) {
$inAnnotationUntilIndex = $endIndex;
}
continue;
}
if (null === $inAnnotationUntilIndex) {
continue;
}
if (!$token->isType(DocLexer::T_COMMA)) {
continue;
}
$token = $tokens[$index - 1];
if ($token->isType(DocLexer::T_NONE)) {
$token->clear();
}
if ($index < \count($tokens) - 1 && !Preg::match('/^\s/', $tokens[$index + 1]->getContent())) {
$tokens->insertAt($index + 1, new Token(DocLexer::T_NONE, ' '));
}
}
}
private function fixAroundAssignments(Tokens $tokens): void
{
$beforeArguments = $this->configuration['before_argument_assignments'];
$afterArguments = $this->configuration['after_argument_assignments'];
$beforeArraysEquals = $this->configuration['before_array_assignments_equals'];
$afterArraysEquals = $this->configuration['after_array_assignments_equals'];
$beforeArraysColon = $this->configuration['before_array_assignments_colon'];
$afterArraysColon = $this->configuration['after_array_assignments_colon'];
$scopes = [];
foreach ($tokens as $index => $token) {
$endScopeType = end($scopes);
if (false !== $endScopeType && $token->isType($endScopeType)) {
array_pop($scopes);
continue;
}
if ($tokens[$index]->isType(DocLexer::T_AT)) {
$scopes[] = DocLexer::T_CLOSE_PARENTHESIS;
continue;
}
if ($tokens[$index]->isType(DocLexer::T_OPEN_CURLY_BRACES)) {
$scopes[] = DocLexer::T_CLOSE_CURLY_BRACES;
continue;
}
if (DocLexer::T_CLOSE_PARENTHESIS === $endScopeType && $token->isType(DocLexer::T_EQUALS)) {
$this->updateSpacesAfter($tokens, $index, $afterArguments);
$this->updateSpacesBefore($tokens, $index, $beforeArguments);
continue;
}
if (DocLexer::T_CLOSE_CURLY_BRACES === $endScopeType) {
if ($token->isType(DocLexer::T_EQUALS)) {
$this->updateSpacesAfter($tokens, $index, $afterArraysEquals);
$this->updateSpacesBefore($tokens, $index, $beforeArraysEquals);
continue;
}
if ($token->isType(DocLexer::T_COLON)) {
$this->updateSpacesAfter($tokens, $index, $afterArraysColon);
$this->updateSpacesBefore($tokens, $index, $beforeArraysColon);
}
}
}
}
private function updateSpacesAfter(Tokens $tokens, int $index, ?bool $insert): void
{
$this->updateSpacesAt($tokens, $index + 1, $index + 1, $insert);
}
private function updateSpacesBefore(Tokens $tokens, int $index, ?bool $insert): void
{
$this->updateSpacesAt($tokens, $index - 1, $index, $insert);
}
private function updateSpacesAt(Tokens $tokens, int $index, int $insertIndex, ?bool $insert): void
{
if (null === $insert) {
return;
}
$token = $tokens[$index];
if ($insert) {
if (!$token->isType(DocLexer::T_NONE)) {
$tokens->insertAt($insertIndex, $token = new Token());
}
$token->setContent(' ');
} elseif ($token->isType(DocLexer::T_NONE)) {
$token->clear();
}
}
}