Skip to content

Commit

Permalink
More tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jan 6, 2017
1 parent f7270c2 commit 9b9e993
Show file tree
Hide file tree
Showing 58 changed files with 762 additions and 46 deletions.
Expand Up @@ -110,6 +110,7 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $comparisonTokenPointe
if (count($leftSideTokens) > 0 & count($rightSideTokens) > 0) {
$phpcsFile->fixer->beginChangeset();
$this->write($phpcsFile, $leftSideTokens, $rightSideTokens);
$phpcsFile->fixer->addContent(key($leftSideTokens), ' ');
$this->write($phpcsFile, $rightSideTokens, $leftSideTokens);
$phpcsFile->fixer->endChangeset();
}
Expand Down
Expand Up @@ -36,7 +36,7 @@ public function getTypeNameFromProjectPath(string $path)
return null;
}

$pathParts = explode(DIRECTORY_SEPARATOR, $path);
$pathParts = preg_split('~[/\\\]~', $path);
$rootNamespace = null;
while (count($pathParts) > 0) {
array_shift($pathParts);
Expand Down
Expand Up @@ -80,7 +80,7 @@ private function fixAlphabeticalOrder(
return $this->compareUseStatements($a, $b);
});

$phpcsFile->fixer->addContent($firstUseStatement->getPointer(), implode(PHP_EOL, array_map(function (UseStatement $useStatement): string {
$phpcsFile->fixer->addContent($firstUseStatement->getPointer(), implode($phpcsFile->eolChar, array_map(function (UseStatement $useStatement): string {
$unqualifiedName = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName($useStatement->getFullyQualifiedTypeName());
if ($unqualifiedName === $useStatement->getNameAsReferencedInFile()) {
return sprintf('use %s;', $useStatement->getFullyQualifiedTypeName());
Expand Down
Expand Up @@ -7,8 +7,12 @@ class EmptyLinesAroundTypeBracesSniff implements \PHP_CodeSniffer_Sniff

const CODE_NO_EMPTY_LINE_AFTER_OPENING_BRACE = 'NoEmptyLineAfterOpeningBrace';

const CODE_MULTIPLE_EMPTY_LINES_AFTER_OPENING_BRACE = 'MultipleEmptyLinesAfterOpeningBrace';

const CODE_NO_EMPTY_LINE_BEFORE_CLOSING_BRACE = 'NoEmptyLineBeforeClosingBrace';

const CODE_MULTIPLE_EMPTY_LINES_BEFORE_CLOSING_BRACE = 'MultipleEmptyLinesBeforeClosingBrace';

/**
* @return int[]
*/
Expand All @@ -34,22 +38,67 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPointer)
$openerToken = $tokens[$openerPointer];
$nextPointerAfterOpeningBrace = $phpcsFile->findNext(T_WHITESPACE, $openerPointer + 1, null, true);
$nextTokenAfterOpeningBrace = $tokens[$nextPointerAfterOpeningBrace];
if ($nextTokenAfterOpeningBrace['line'] !== $openerToken['line'] + 2) {
$phpcsFile->addError(sprintf(

$lines = $nextTokenAfterOpeningBrace['line'] - $openerToken['line'] - 1;
if ($lines === 0) {
$fix = $phpcsFile->addFixableError(sprintf(
'There must be one empty line after %s opening brace.',
$typeToken['content']
), $openerPointer, self::CODE_NO_EMPTY_LINE_AFTER_OPENING_BRACE);

if ($fix) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addNewline($openerPointer);
$phpcsFile->fixer->endChangeset();
}
} elseif ($lines > 1) {
$fix = $phpcsFile->addFixableError(sprintf(
'There must be one empty line after %s opening brace.',
$typeToken['content']
), $nextPointerAfterOpeningBrace, self::CODE_NO_EMPTY_LINE_AFTER_OPENING_BRACE);
), $openerPointer, self::CODE_MULTIPLE_EMPTY_LINES_AFTER_OPENING_BRACE);

if ($fix) {
$phpcsFile->fixer->beginChangeset();
for ($i = $openerPointer + 3; $i < $nextPointerAfterOpeningBrace; $i++) {
if ($tokens[$i]['content'] !== $phpcsFile->eolChar) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
}
}

$closerPointer = $typeToken['scope_closer'];
$closerToken = $tokens[$closerPointer];
$previousPointerAfterClosingBrace = $phpcsFile->findPrevious(T_WHITESPACE, $closerPointer - 1, null, true);
$previousTokenAfterClosingBrace = $tokens[$previousPointerAfterClosingBrace];
if ($previousTokenAfterClosingBrace['line'] !== $closerToken['line'] - 2) {
$phpcsFile->addError(sprintf(
$previousPointerBeforeClosingBrace = $phpcsFile->findPrevious(T_WHITESPACE, $closerPointer - 1, null, true);
$previousTokenBeforeClosingBrace = $tokens[$previousPointerBeforeClosingBrace];

$lines = $closerToken['line'] - $previousTokenBeforeClosingBrace['line'] - 1;
if ($lines === 0) {
$fix = $phpcsFile->addFixableError(sprintf(
'There must be one empty line before %s closing brace.',
$typeToken['content']
), $closerPointer, self::CODE_NO_EMPTY_LINE_BEFORE_CLOSING_BRACE);

if ($fix) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addNewlineBefore($closerPointer);
$phpcsFile->fixer->endChangeset();
}
} elseif ($lines > 1) {
$fix = $phpcsFile->addFixableError(sprintf(
'There must be one empty line before %s closing brace.',
$typeToken['content']
), $previousPointerAfterClosingBrace, self::CODE_NO_EMPTY_LINE_BEFORE_CLOSING_BRACE);
), $closerPointer, self::CODE_MULTIPLE_EMPTY_LINES_BEFORE_CLOSING_BRACE);

if ($fix) {
$phpcsFile->fixer->beginChangeset();
for ($i = $previousPointerBeforeClosingBrace + 3; $i < $closerPointer; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions build.xml
Expand Up @@ -28,8 +28,6 @@
passthru="true"
checkreturn="true"
>
<arg value="--exclude"/>
<arg path="tests/Sniffs/TypeHints/data"/>
<arg path="SlevomatCodingStandard" />
<arg path="tests" />
</exec>
Expand Down
23 changes: 23 additions & 0 deletions tests/Helpers/SniffSettingsHelperTest.php
Expand Up @@ -20,4 +20,27 @@ public function testNormalizeArray()
]));
}

public function testNormalizeAssociativeArray()
{
$this->assertSame([
'app/ui' => 'Slevomat\UI',
'app' => 'Slevomat',
'build/SlevomatSniffs/Sniffs' => 'SlevomatSniffs\Sniffs',
'tests/ui' => 'Slevomat\UI',
'tests' => 'Slevomat',
], SniffSettingsHelper::normalizeAssociativeArray([
' ' => ' ',
'app/ui' => 'Slevomat\UI',
' ' => ' ',
'app' => 'Slevomat',
' ' => ' ',
'build/SlevomatSniffs/Sniffs' => 'SlevomatSniffs\Sniffs',
' ' => ' ',
'tests/ui' => 'Slevomat\UI',
' ' => ' ',
'tests' => 'Slevomat',
' ' => ' ',
]));
}

}
2 changes: 1 addition & 1 deletion tests/Helpers/TokenHelperTest.php
Expand Up @@ -230,7 +230,7 @@ public function testGetContent()
$openParenthesisTokenPointer = $codeSnifferFile->findNext(T_OPEN_PARENTHESIS, 0);
$this->assertTokenPointer(T_OPEN_PARENTHESIS, 4, $codeSnifferFile, $openParenthesisTokenPointer);
$content = TokenHelper::getContent($codeSnifferFile, $variableTokenPointer, $openParenthesisTokenPointer);
$this->assertSame(sprintf('$i++;%sfoo', "\n"), $content); // intentionally "\n" instead of PHP_EOL
$this->assertSame(sprintf('$i++;%sfoo', $codeSnifferFile->eolChar), $content);
}

public function testGetLastTokenPointer()
Expand Down
6 changes: 6 additions & 0 deletions tests/Sniffs/Arrays/TrailingArrayCommaSniffTest.php
Expand Up @@ -16,4 +16,10 @@ public function testCheckFile()
$this->assertNoSniffError($resultFile, 28);
}

public function testFixable()
{
$report = $this->checkFile(__DIR__ . '/data/fixableTrailingCommas.php', [], [TrailingArrayCommaSniff::CODE_MISSING_TRAILING_COMMA]);
$this->assertAllFixedInFile($report);
}

}
29 changes: 29 additions & 0 deletions tests/Sniffs/Arrays/data/fixableTrailingCommas.fixed.php
@@ -0,0 +1,29 @@
<?php

[1, 2, 3];

[
1,
2,
[
3,
5,
],
5,
];

[
1,
2,
3,
];

[
1,
2,
[
3,
5,
],
5,
];
29 changes: 29 additions & 0 deletions tests/Sniffs/Arrays/data/fixableTrailingCommas.php
@@ -0,0 +1,29 @@
<?php

[1, 2, 3];

[
1,
2,
[
3,
5,
],
5
];

[
1,
2,
3
];

[
1,
2,
[
3,
5
],
5,
];
17 changes: 16 additions & 1 deletion tests/Sniffs/Classes/UnusedPrivateElementsSniffTest.php
Expand Up @@ -5,7 +5,7 @@
class UnusedPrivateElementsSniffTest extends \SlevomatCodingStandard\Sniffs\TestCase
{

public function testCheckFile()
public function testErrors()
{
$resultFile = $this->checkFile(__DIR__ . '/data/ClassWithSomeUnusedElements.php', [
'alwaysUsedPropertiesAnnotations' => [
Expand Down Expand Up @@ -66,4 +66,19 @@ public function testCheckFile()
$this->assertNoSniffError($resultFile, 91);
}

public function testOnlyPublicElements()
{
$this->assertNoSniffErrorInFile($this->checkFile(__DIR__ . '/data/ClassWithOnlyPublicElements.php'));
}

public function testClassWithSpecialThis()
{
$this->assertNoSniffErrorInFile($this->checkFile(__DIR__ . '/data/ClassWithSpecialThis.php'));
}

public function testClassWithSpecialSelf()
{
$this->assertNoSniffErrorInFile($this->checkFile(__DIR__ . '/data/ClassWithSpecialSelf.php'));
}

}
24 changes: 24 additions & 0 deletions tests/Sniffs/Classes/data/ClassWithOnlyPublicElements.php
@@ -0,0 +1,24 @@
<?php

class ClassWithOnlyPublicElements
{

public $publicProperty;

public function foo()
{
$this->usedProperty->foo();
$this->usedPrivateMethod();
}

public function publicMethod()
{

}

public static function staticPublicMethod()
{
self::usedStaticPrivateMethod();
}

}
23 changes: 23 additions & 0 deletions tests/Sniffs/Classes/data/ClassWithSpecialSelf.php
@@ -0,0 +1,23 @@
<?php

class ClassWithSpecialSelf
{

const CONSTANT = 0;

const CONSTANT2 = 0;

private $property;

public function specialSelf()
{
self::${'CONSTANT'} = self::CONSTANT2;
$this->property = self::CONSTANT;
}

public function returnProperty()
{
return $this->property;
}

}
23 changes: 23 additions & 0 deletions tests/Sniffs/Classes/data/ClassWithSpecialThis.php
@@ -0,0 +1,23 @@
<?php

class ClassWithSpecialThis
{

public $publicProperty;

public $publicProperty2;

private $property;

public function specialThis()
{
$this->{'publicProperty'} = $this->publicProperty2;
$this->property = $this;
}

public function returnProperty()
{
return $this->property;
}

}
6 changes: 6 additions & 0 deletions tests/Sniffs/ControlStructures/YodaComparisonSniffTest.php
Expand Up @@ -31,4 +31,10 @@ public function testIncorrectFile(int $lineNumber)
$this->assertSniffError($resultFile, $lineNumber, YodaComparisonSniff::CODE_YODA_COMPARISON);
}

public function testFixable()
{
$report = $this->checkFile(__DIR__ . '/data/fixableYodaComparisons.php', [], [YodaComparisonSniff::CODE_YODA_COMPARISON]);
$this->assertAllFixedInFile($report);
}

}
@@ -0,0 +1,28 @@
<?php

$foo === 123;
$foo === true;
$foo === false;
$foo === null;
$foo === [];
BAR === 123;
Foo::BAR === 123;
Foo::BAR === 123.0;
$e === \Foo\Bar::BAR;
foo() === Foo::BAR;
foo() + 2 === Foo::BAR;
$foo === Foo::BAR;
$foo + 2 === Foo::BAR;
$this->foo() === Foo::BAR;
$foo === -1;
$foo === +1;
(foo() === BAR|| (
Foo::BAR === ['test'])) ? Foo::BAR === 123.0: $foo === null;

if (
$foo($bar) === [Foo::BAR, Foo::BAZ] && (
$bar === true ||
$bar === null
)
) {
}

0 comments on commit 9b9e993

Please sign in to comment.