Skip to content

Commit

Permalink
Merge branch 'master' into symfony-no_unreachable_default_argument_value
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Mar 11, 2024
2 parents d300945 + ac2bbea commit 56ec609
Show file tree
Hide file tree
Showing 83 changed files with 754 additions and 264 deletions.
2 changes: 1 addition & 1 deletion dev-tools/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"mi-schi/phpmd-extension": "^4.3.0",
"phpmd/phpmd": "^2.15.0",
"phpstan/extension-installer": "^1.3.1",
"phpstan/phpstan": "^1.10.59",
"phpstan/phpstan": "^1.10.60",
"phpstan/phpstan-phpunit": "^1.3.16",
"phpstan/phpstan-strict-rules": "^1.5.2"
},
Expand Down
10 changes: 5 additions & 5 deletions dev-tools/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion doc/ruleSets/PhpCsFixer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,4 @@ Rules
Disabled rules
--------------

- `nullable_type_declaration_for_default_null_value <./../rules/function_notation/nullable_type_declaration_for_default_null_value.rst>`_
- `single_line_throw <./../rules/function_notation/single_line_throw.rst>`_
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ With configuration: ``['use_nullable_type_declaration' => false]``.
Rule sets
---------

The rule is part of the following rule set:
The rule is part of the following rule sets:

- `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_
- `@Symfony <./../../ruleSets/Symfony.rst>`_

References
Expand Down
2 changes: 1 addition & 1 deletion phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ parameters:
-
message: '#^Method PhpCsFixer\\Tests\\.+::provide.+Cases\(\) return type has no value type specified in iterable type iterable\.$#'
path: tests
count: 1021
count: 1020
tipsOfTheDay: false
tmpDir: dev-tools/phpstan/cache
2 changes: 1 addition & 1 deletion src/Fixer/Basic/BracesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function getSuccessorsNames(): array
return array_keys($this->proxyFixers);
}

public function configure(array $configuration = null): void
public function configure(array $configuration): void
{
parent::configure($configuration);

Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Naming/NoHomoglyphNamesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void

$replaced = Preg::replaceCallback('/[^[:ascii:]]/u', static fn (array $matches): string => self::$replacements[$matches[0]] ?? $matches[0], $token->getContent(), -1, $count);

if ($count) {
if ($count > 0) {
$tokens->offsetSet($index, new Token([$token->getId(), $replaced]));
}
}
Expand Down
97 changes: 60 additions & 37 deletions src/Fixer/Operator/NoUselessConcatOperatorFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

/**
* @phpstan-type _ConcatOperandType array{
* start: int,
* end: int,
* type: self::STR_*,
* }
*/
final class NoUselessConcatOperatorFixer extends AbstractFixer implements ConfigurableFixerInterface
{
private const STR_DOUBLE_QUOTE = 0;
Expand All @@ -47,7 +54,7 @@ public function getDefinition(): FixerDefinitionInterface
* {@inheritdoc}
*
* Must run before DateTimeCreateFromFormatCallFixer, EregToPregFixer, PhpUnitDedicateAssertInternalTypeFixer, RegularCallableCallFixer, SetTypeToCastFixer.
* Must run after NoBinaryStringFixer, SingleQuoteFixer.
* Must run after ExplicitStringVariableFixer, NoBinaryStringFixer, SingleQuoteFixer.
*/
public function getPriority(): int
{
Expand Down Expand Up @@ -105,16 +112,8 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn
}

/**
* @param array{
* start: int,
* end: int,
* type: self::STR_*,
* } $firstOperand
* @param array{
* start: int,
* end: int,
* type: self::STR_*,
* } $secondOperand
* @param _ConcatOperandType $firstOperand
* @param _ConcatOperandType $secondOperand
*/
private function fixConcatOperation(Tokens $tokens, array $firstOperand, int $concatIndex, array $secondOperand): void
{
Expand All @@ -130,6 +129,10 @@ private function fixConcatOperation(Tokens $tokens, array $firstOperand, int $co
}

if (self::STR_DOUBLE_QUOTE_VAR === $firstOperand['type'] && self::STR_DOUBLE_QUOTE_VAR === $secondOperand['type']) {
if ($this->operandsCanNotBeMerged($tokens, $firstOperand, $secondOperand)) {
return;
}

$this->mergeConstantEscapedStringVarOperands($tokens, $firstOperand, $concatIndex, $secondOperand);

return;
Expand All @@ -146,6 +149,10 @@ private function fixConcatOperation(Tokens $tokens, array $firstOperand, int $co
[$operand1, $operand2] = $operandPair;

if (self::STR_DOUBLE_QUOTE_VAR === $operand1['type'] && self::STR_DOUBLE_QUOTE === $operand2['type']) {
if ($this->operandsCanNotBeMerged($tokens, $operand1, $operand2)) {
return;
}

$this->mergeConstantEscapedStringVarOperands($tokens, $firstOperand, $concatIndex, $secondOperand);

return;
Expand All @@ -169,6 +176,10 @@ private function fixConcatOperation(Tokens $tokens, array $firstOperand, int $co
$operantContent = $tokens[$operand2['start']]->getContent();

if ($this->isSimpleQuotedStringContent($operantContent)) {
if ($this->operandsCanNotBeMerged($tokens, $operand1, $operand2)) {
return;
}

$this->mergeConstantEscapedStringVarOperands($tokens, $firstOperand, $concatIndex, $secondOperand);
}

Expand All @@ -180,11 +191,7 @@ private function fixConcatOperation(Tokens $tokens, array $firstOperand, int $co
/**
* @param -1|1 $direction
*
* @return null|array{
* start: int,
* end: int,
* type: self::STR_*,
* }
* @return null|_ConcatOperandType
*/
private function getConcatOperandType(Tokens $tokens, int $index, int $direction): ?array
{
Expand Down Expand Up @@ -216,16 +223,8 @@ private function getConcatOperandType(Tokens $tokens, int $index, int $direction
}

/**
* @param array{
* start: int,
* end: int,
* type: self::STR_*,
* } $firstOperand
* @param array{
* start: int,
* end: int,
* type: self::STR_*,
* } $secondOperand
* @param _ConcatOperandType $firstOperand
* @param _ConcatOperandType $secondOperand
*/
private function mergeConstantEscapedStringOperands(
Tokens $tokens,
Expand All @@ -249,24 +248,16 @@ private function mergeConstantEscapedStringOperands(
}

/**
* @param array{
* start: int,
* end: int,
* type: self::STR_*,
* } $firstOperand
* @param array{
* start: int,
* end: int,
* type: self::STR_*,
* } $secondOperand
* @param _ConcatOperandType $firstOperand
* @param _ConcatOperandType $secondOperand
*/
private function mergeConstantEscapedStringVarOperands(
Tokens $tokens,
array $firstOperand,
int $concatOperatorIndex,
array $secondOperand
): void {
// build uo the new content
// build up the new content
$newContent = '';

foreach ([$firstOperand, $secondOperand] as $operant) {
Expand Down Expand Up @@ -336,4 +327,36 @@ private function containsLinebreak(Tokens $tokens, int $startIndex, int $endInde

return false;
}

/**
* @param _ConcatOperandType $firstOperand
* @param _ConcatOperandType $secondOperand
*/
private function operandsCanNotBeMerged(Tokens $tokens, array $firstOperand, array $secondOperand): bool
{
// If the first operand does not end with a variable, no variables would be broken by concatenation.
if (self::STR_DOUBLE_QUOTE_VAR !== $firstOperand['type']) {
return false;
}
if (!$tokens[$firstOperand['end'] - 1]->isGivenKind(T_VARIABLE)) {
return false;
}

$allowedPatternsForSecondOperand = [
'/^\s.*/', // e.g. " foo", ' bar', " $baz"
'/^-(?!\>)/', // e.g. "-foo", '-bar', "-$baz"
];

// If the first operand ends with a variable, the second operand should match one of the allowed patterns.
// Otherwise, the concatenation can break a variable in the first operand.
foreach ($allowedPatternsForSecondOperand as $allowedPattern) {
$secondOperandInnerContent = substr($tokens->generatePartialCode($secondOperand['start'], $secondOperand['end']), 1, -1);

if (Preg::match($allowedPattern, $secondOperandInnerContent)) {
return false;
}
}

return true;
}
}
2 changes: 1 addition & 1 deletion src/Fixer/PhpTag/FullOpeningTagFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
// replace all <? with <?php to replace all short open tags even without short_open_tag option enabled
$newContent = Preg::replace('/<\?(?:phP|pHp|pHP|Php|PhP|PHp|PHP)?(\s|$)/', '<?php$1', $content, -1, $count);

if (!$count) {
if (0 === $count) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Phpdoc/PhpdocNoUselessInheritdocFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private function fixToken(Tokens $tokens, int $tokenIndex): void
$count
);

if ($count) {
if ($count > 0) {
$tokens[$tokenIndex] = new Token([T_DOC_COMMENT, $content]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Phpdoc/PhpdocToCommentFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function returnClassName() {
);
}

public function configure(array $configuration = null): void
public function configure(array $configuration): void
{
parent::configure($configuration);

Expand Down
3 changes: 2 additions & 1 deletion src/Fixer/StringNotation/ExplicitStringVariableFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ public function getDefinition(): FixerDefinitionInterface
/**
* {@inheritdoc}
*
* Must run before NoUselessConcatOperatorFixer.
* Must run after BacktickToShellExecFixer.
*/
public function getPriority(): int
{
return 0;
return 6;
}

public function isCandidate(Tokens $tokens): bool
Expand Down
6 changes: 3 additions & 3 deletions src/Fixer/Whitespace/HeredocIndentationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private function fixIndentation(Tokens $tokens, int $start, int $end): void
$content = Preg::replace('/(?<=\v)(?!'.$currentIndent.')\h+/', '', $content);
}

$regexEnd = $last && !$currentIndent ? '(?!\v|$)' : '(?!\v)';
$regexEnd = $last && '' === $currentIndent ? '(?!\v|$)' : '(?!\v)';
$content = Preg::replace('/(?<=\v)'.$currentIndent.$regexEnd.'/', $indent, $content);

$tokens[$index] = new Token([$tokens[$index]->getId(), $content]);
Expand All @@ -156,9 +156,9 @@ private function fixIndentation(Tokens $tokens, int $start, int $end): void

$content = $tokens[$index]->getContent();

if (!\in_array($content[0], ["\r", "\n"], true) && (!$currentIndent || str_starts_with($content, $currentIndent))) {
if (!\in_array($content[0], ["\r", "\n"], true) && ('' === $currentIndent || str_starts_with($content, $currentIndent))) {
$content = $indent.substr($content, $currentIndentLength);
} elseif ($currentIndent) {
} elseif ('' !== $currentIndent) {
$content = Preg::replace('/^(?!'.$currentIndent.')\h+/', '', $content);
}

Expand Down
Loading

0 comments on commit 56ec609

Please sign in to comment.