Skip to content

Commit

Permalink
TokensAnalyzer.php - Fix T_ENCAPSED_AND_WHITESPACE handling in isBina…
Browse files Browse the repository at this point in the history
…ryOperator
  • Loading branch information
SpacePossum committed Oct 31, 2021
1 parent 17d66a7 commit 79f0872
Show file tree
Hide file tree
Showing 27 changed files with 74 additions and 97 deletions.
2 changes: 1 addition & 1 deletion src/AbstractPhpdocTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$doc = new DocBlock($token->getContent());
$annotations = $doc->getAnnotationsOfType($this->tags);

if (empty($annotations)) {
if (0 === \count($annotations)) {
continue;
}

Expand Down
6 changes: 2 additions & 4 deletions src/Fixer/ClassNotation/NoPhp4ConstructorFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,11 @@ private function fixConstructor(Tokens $tokens, string $className, int $classSta
$php4 = $this->findFunction($tokens, $className, $classStart, $classEnd);

if (null === $php4) {
// no PHP4-constructor!
return;
return; // no PHP4-constructor!
}

if (!empty($php4['modifiers'][T_ABSTRACT]) || !empty($php4['modifiers'][T_STATIC])) {
// PHP4 constructor can't be abstract or static
return;
return; // PHP4 constructor can't be abstract or static
}

$php5 = $this->findFunction($tokens, '__construct', $classStart, $classEnd);
Expand Down
2 changes: 2 additions & 0 deletions src/Fixer/Import/OrderedImportsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ private function getNewOrder(array $uses, Tokens $tokens): array
if (null !== $this->configuration['imports_order']) {
// Grouping indexes by import type.
$groupedByTypes = [];

foreach ($indexes as $startIndex => $item) {
$groupedByTypes[$item['importType']][$startIndex] = $item;
}
Expand All @@ -488,6 +489,7 @@ private function getNewOrder(array $uses, Tokens $tokens): array

// Ordering groups
$sortedGroups = [];

foreach ($this->configuration['imports_order'] as $type) {
if (isset($groupedByTypes[$type]) && !empty($groupedByTypes[$type])) {
foreach ($groupedByTypes[$type] as $startIndex => $item) {
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/PhpUnit/PhpUnitNamespacedFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ private function generateReplacement(string $originalClassName): Tokens
}

$parts = explode($delimiter, $string);

$tokensArray = [];

while (!empty($parts)) {
$tokensArray[] = new Token([T_STRING, array_shift($parts)]);
if (!empty($parts)) {
Expand Down
7 changes: 5 additions & 2 deletions src/Fixer/PhpUnit/PhpUnitSizeClassFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ private function createDocBlock(Tokens $tokens, int $docBlockIndex): void
private function updateDocBlockIfNeeded(Tokens $tokens, int $docBlockIndex): void
{
$doc = new DocBlock($tokens[$docBlockIndex]->getContent());
if (!empty($this->filterDocBlock($doc))) {

if (0 !== \count($this->filterDocBlock($doc))) {
return;
}

$doc = $this->makeDocBlockMultiLineIfNeeded($doc, $tokens, $docBlockIndex);
$lines = $this->addSizeAnnotation($doc, $tokens, $docBlockIndex);
$lines = implode('', $lines);
Expand All @@ -133,7 +135,8 @@ private function addSizeAnnotation(DocBlock $docBlock, Tokens $tokens, int $docB
private function makeDocBlockMultiLineIfNeeded(DocBlock $doc, Tokens $tokens, int $docBlockIndex): DocBlock
{
$lines = $doc->getLines();
if (1 === \count($lines) && empty($this->filterDocBlock($doc))) {

if (1 === \count($lines) && 0 === \count($this->filterDocBlock($doc))) {
$lines = $this->splitUpDocBlock($lines, $tokens, $docBlockIndex);

return new DocBlock(implode('', $lines));
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/PhpUnit/PhpUnitTestAnnotationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,6 @@ private function addTestAnnotation(array $lines, Tokens $tokens, int $docBlockIn

private function doesDocBlockContainTest(DocBlock $doc): bool
{
return !empty($doc->getAnnotationsOfType('test'));
return 0 !== \count($doc->getAnnotationsOfType('test'));
}
}
2 changes: 1 addition & 1 deletion src/Fixer/PhpUnit/PhpUnitTestClassRequiresCoversFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected function applyPhpUnitClassFix(Tokens $tokens, int $startIndex, int $en
$doc = new DocBlock($docContent);

// skip if already has annotation
if (!empty($doc->getAnnotationsOfType([
if (0 !== \count($doc->getAnnotationsOfType([
'covers',
'coversDefaultClass',
'coversNothing',
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Phpdoc/GeneralPhpdocAnnotationRemoveFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$annotations = $doc->getAnnotationsOfType($this->configuration['annotations']);

// nothing to do if there are no annotations
if (empty($annotations)) {
if (0 === \count($annotations)) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Phpdoc/PhpdocAnnotationWithoutDotFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$doc = new DocBlock($token->getContent());
$annotations = $doc->getAnnotations();

if (empty($annotations)) {
if (0 === \count($annotations)) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Phpdoc/PhpdocNoEmptyReturnFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$doc = new DocBlock($token->getContent());
$annotations = $doc->getAnnotationsOfType('return');

if (empty($annotations)) {
if (0 === \count($annotations)) {
continue;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Fixer/Phpdoc/PhpdocOrderFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ private function moveParamAnnotations(string $content): string
$params = $doc->getAnnotationsOfType('param');

// nothing to do if there are no param annotations
if (empty($params)) {
if (0 === \count($params)) {
return $content;
}

$others = $doc->getAnnotationsOfType(['throws', 'return']);

if (empty($others)) {
if (0 === \count($others)) {
return $content;
}

Expand Down Expand Up @@ -137,14 +137,14 @@ private function moveReturnAnnotations(string $content): string
$returns = $doc->getAnnotationsOfType('return');

// nothing to do if there are no return annotations
if (empty($returns)) {
if (0 === \count($returns)) {
return $content;
}

$others = $doc->getAnnotationsOfType(['param', 'throws']);

// nothing to do if there are no other annotations
if (empty($others)) {
if (0 === \count($others)) {
return $content;
}

Expand Down
13 changes: 9 additions & 4 deletions src/Tokenizer/TokensAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ public function isConstantInvocation(int $index): bool
// check for `extends`/`implements`/`use` list
if ($this->tokens[$prevIndex]->equals(',')) {
$checkIndex = $prevIndex;

while ($this->tokens[$checkIndex]->equalsAny([',', [T_AS], [CT::T_NAMESPACE_OPERATOR], [T_NS_SEPARATOR], [T_STRING]])) {
$checkIndex = $this->tokens->getPrevMeaningfulToken($checkIndex);
}
Expand Down Expand Up @@ -382,6 +383,7 @@ public function isConstantInvocation(int $index): bool
// check for non-capturing catches
if ($this->tokens[$prevIndex]->equals('(')) {
$prevPrevIndex = $this->tokens->getPrevMeaningfulToken($prevIndex);

if ($this->tokens[$prevPrevIndex]->isGivenKind(T_CATCH)) {
return false;
}
Expand Down Expand Up @@ -426,11 +428,13 @@ public function isUnaryPredecessorOperator(int $index): bool
static $potentialBinaryOperator = ['+', '-', '&', [CT::T_RETURN_REF]];

static $otherOperators;

if (null === $otherOperators) {
$otherOperators = ['!', '~', '@', [T_ELLIPSIS]];
}

static $disallowedPrevTokens;

if (null === $disallowedPrevTokens) {
$disallowedPrevTokens = [
']',
Expand Down Expand Up @@ -522,6 +526,7 @@ public function isBinaryOperator(int $index): bool
];

static $arrayOperators;

if (null === $arrayOperators) {
$arrayOperators = [
T_AND_EQUAL => true, // &=
Expand Down Expand Up @@ -564,10 +569,6 @@ public function isBinaryOperator(int $index): bool
$tokens = $this->tokens;
$token = $tokens[$index];

if (isset($potentialUnaryNonArrayOperators[$token->getContent()])) {
return !$this->isUnaryPredecessorOperator($index);
}

if ($token->isArray()) {
return isset($arrayOperators[$token->getId()]);
}
Expand All @@ -576,6 +577,10 @@ public function isBinaryOperator(int $index): bool
return true;
}

if (isset($potentialUnaryNonArrayOperators[$token->getContent()])) {
return !$this->isUnaryPredecessorOperator($index);
}

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static function (int $a, int $b): int {
*/
public static function naturalLanguageJoinWithBackticks(array $names): string
{
if (empty($names)) {
if (0 === \count($names)) {
throw new \InvalidArgumentException('Array of names cannot be empty.');
}

Expand Down
2 changes: 1 addition & 1 deletion tests/DocBlock/AnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public function testSetTypesOnBadTag(): void
public function testGetTagsWithTypes(): void
{
$tags = Annotation::getTagsWithTypes();
static::assertIsArray($tags);

foreach ($tags as $tag) {
static::assertIsString($tag);
static::assertNotEmpty($tag);
Expand Down
24 changes: 8 additions & 16 deletions tests/DocBlock/DocBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public function testEmptyContent(): void
public function testGetLines(): void
{
$doc = new DocBlock(self::$sample);
$lines = $doc->getLines();

static::assertIsArray($doc->getLines());
static::assertCount(15, $doc->getLines());
static::assertCount(15, $lines);

foreach ($doc->getLines() as $index => $line) {
foreach ($lines as $index => $line) {
static::assertInstanceOf(\PhpCsFixer\DocBlock\Line::class, $line);
static::assertSame($doc->getLine($index), $line);
}
Expand All @@ -80,13 +80,13 @@ public function testGetLines(): void
public function testGetAnnotations(): void
{
$doc = new DocBlock(self::$sample);
$annotations = $doc->getAnnotations();

static::assertIsArray($doc->getAnnotations());
static::assertCount(5, $doc->getAnnotations());
static::assertCount(5, $annotations);

foreach ($doc->getAnnotations() as $index => $annotations) {
static::assertInstanceOf(\PhpCsFixer\DocBlock\Annotation::class, $annotations);
static::assertSame($doc->getAnnotation($index), $annotations);
foreach ($annotations as $index => $annotation) {
static::assertInstanceOf(\PhpCsFixer\DocBlock\Annotation::class, $annotation);
static::assertSame($doc->getAnnotation($index), $annotation);
}

static::assertEmpty($doc->getAnnotation(5));
Expand All @@ -95,10 +95,8 @@ public function testGetAnnotations(): void
public function testGetAnnotationsOfTypeParam(): void
{
$doc = new DocBlock(self::$sample);

$annotations = $doc->getAnnotationsOfType('param');

static::assertIsArray($annotations);
static::assertCount(3, $annotations);

$first = ' * @param string $hello
Expand All @@ -117,10 +115,8 @@ public function testGetAnnotationsOfTypeParam(): void
public function testGetAnnotationsOfTypeThrows(): void
{
$doc = new DocBlock(self::$sample);

$annotations = $doc->getAnnotationsOfType('throws');

static::assertIsArray($annotations);
static::assertCount(1, $annotations);

$content = ' * @throws \Exception asdnjkasd
Expand All @@ -134,10 +130,8 @@ public function testGetAnnotationsOfTypeThrows(): void
public function testGetAnnotationsOfTypeReturn(): void
{
$doc = new DocBlock(self::$sample);

$annotations = $doc->getAnnotationsOfType('return');

static::assertIsArray($annotations);
static::assertCount(1, $annotations);

$content = ' * @return void
Expand All @@ -149,10 +143,8 @@ public function testGetAnnotationsOfTypeReturn(): void
public function testGetAnnotationsOfTypeFoo(): void
{
$doc = new DocBlock(self::$sample);

$annotations = $doc->getAnnotationsOfType('foo');

static::assertIsArray($annotations);
static::assertCount(0, $annotations);
}

Expand Down
5 changes: 1 addition & 4 deletions tests/FixerDefinition/FileSpecificCodeSampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ final class FileSpecificCodeSampleTest extends TestCase
{
public function testImplementsFileSpecificCodeSampleInterface(): void
{
$sample = new FileSpecificCodeSample(
file_get_contents(__FILE__),
new \SplFileInfo(__FILE__)
);
$sample = new FileSpecificCodeSample(file_get_contents(__FILE__), new \SplFileInfo(__FILE__));

static::assertInstanceOf(\PhpCsFixer\FixerDefinition\FileSpecificCodeSampleInterface::class, $sample);
}
Expand Down
1 change: 0 additions & 1 deletion tests/RuleSet/RuleSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public function testGetBuildInSetDefinitionNames(): void
{
$setNames = RuleSets::getSetDefinitionNames();

static::assertIsArray($setNames);
static::assertNotEmpty($setNames);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/RuleSet/RuleSetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ private static function getFixerByName(string $name): AbstractFixer

$fixers = $factory->getFixers();

if (empty($fixers)) {
if (0 === \count($fixers)) {
throw new \RuntimeException('FixerFactory unexpectedly returned empty array.');
}

Expand Down
9 changes: 2 additions & 7 deletions tests/Test/AbstractFixerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
use PhpCsFixer\Fixer\Whitespace\SingleBlankLineAtEofFixer;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
use PhpCsFixer\FixerDefinition\CodeSampleInterface;
use PhpCsFixer\FixerDefinition\FileSpecificCodeSampleInterface;
Expand Down Expand Up @@ -92,8 +91,6 @@ protected function tearDown(): void

final public function testIsRisky(): void
{
static::assertIsBool($this->fixer->isRisky(), sprintf('Return type for ::isRisky of "%s" is invalid.', $this->fixer->getName()));

if ($this->fixer->isRisky()) {
self::assertValidDescription($this->fixer->getName(), 'risky description', $this->fixer->getDefinition()->getRiskyDescription());
} else {
Expand Down Expand Up @@ -126,23 +123,23 @@ final public function testFixerDefinitions(): void

$configSamplesProvided = [];
$dummyFileInfo = new StdinFileInfo();

foreach ($samples as $sampleCounter => $sample) {
static::assertInstanceOf(CodeSampleInterface::class, $sample, sprintf('[%s] Sample #%d', $fixerName, $sampleCounter));
static::assertIsInt($sampleCounter);

$code = $sample->getCode();

static::assertIsString($code, sprintf('[%s] Sample #%d', $fixerName, $sampleCounter));
static::assertNotEmpty($code, sprintf('[%s] Sample #%d', $fixerName, $sampleCounter));

if (!($this->fixer instanceof SingleBlankLineAtEofFixer)) {
static::assertStringEndsWith("\n", $code, sprintf('[%s] Sample #%d must end with linebreak', $fixerName, $sampleCounter));
}

$config = $sample->getConfiguration();

if (null !== $config) {
static::assertTrue($fixerIsConfigurable, sprintf('[%s] Sample #%d has configuration, but the fixer is not configurable.', $fixerName, $sampleCounter));
static::assertIsArray($config, sprintf('[%s] Sample #%d configuration must be an array or null.', $fixerName, $sampleCounter));

$configSamplesProvided[$sampleCounter] = $config;
} elseif ($fixerIsConfigurable) {
Expand Down Expand Up @@ -320,8 +317,6 @@ final public function testFixerConfigurationDefinitions(): void

$configurationDefinition = $this->fixer->getConfigurationDefinition();

static::assertInstanceOf(FixerConfigurationResolverInterface::class, $configurationDefinition);

foreach ($configurationDefinition->getOptions() as $option) {
static::assertInstanceOf(FixerOptionInterface::class, $option);
static::assertNotEmpty($option->getDescription());
Expand Down
1 change: 0 additions & 1 deletion tests/Test/AbstractTransformerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function testGetName(): void
{
$name = $this->transformer->getName();

static::assertIsString($name);
static::assertMatchesRegularExpression('/^[a-z]+[a-z_]*[a-z]$/', $name);
}

Expand Down
Loading

0 comments on commit 79f0872

Please sign in to comment.