Skip to content

Commit

Permalink
Merge pull request #64 from object-calisthenics/prepare-release
Browse files Browse the repository at this point in the history
[tests] use AbstractSniffRunner, no custom; bump to CodeSniffer 3.0
  • Loading branch information
Tomáš Votruba committed Jun 17, 2017
2 parents ad32bdd + e653869 commit 2aaf984
Show file tree
Hide file tree
Showing 61 changed files with 589 additions and 739 deletions.
13 changes: 7 additions & 6 deletions composer.json
Expand Up @@ -5,15 +5,16 @@
"type": "phpcodesniffer-standard",
"require": {
"php": "^7.1",
"squizlabs/php_codesniffer": "dev-master",
"squizlabs/php_codesniffer": "^3.0.1",
"nette/utils": "^2.4"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"phpstan/phpstan": "^0.6",
"phpstan/phpstan": "^0.7",
"tracy/tracy": "^2.4",
"symplify/easy-coding-standard": "dev-master",
"slevomat/coding-standard": "dev-phpcs3"
"symplify/easy-coding-standard": "^2.0",
"symplify/coding-standard": "^2.0",
"slevomat/coding-standard": "^3.0"
},
"autoload": {
"psr-4": {
Expand All @@ -28,7 +29,7 @@
"scripts": {
"complete-check": ["@phpstan", "@check-cs", "phpunit"],
"phpstan": "phpstan analyze src --level 5 --configuration phpstan.neon",
"check-cs": "vendor/bin/easy-coding-standard check src tests",
"fix-cs": "vendor/bin/easy-coding-standard check src tests --fix"
"check-cs": "easy-coding-standard check src tests",
"fix-cs": "easy-coding-standard check src tests --fix"
}
}
42 changes: 10 additions & 32 deletions easy-coding-standard.neon
Expand Up @@ -20,18 +20,17 @@ checkers:
allowedShortNames: ["i", "id", "to", "up"]

# Rule 7: Keep your classes small (with default values)
ObjectCalisthenics\Sniffs\Metrics\ClassTraitAndInterfaceLength:
ObjectCalisthenics\Sniffs\Files\ClassTraitAndInterfaceLengthSniff:
maxLength: 200
ObjectCalisthenics\Sniffs\Files\FunctionLength:
ObjectCalisthenics\Sniffs\Files\FunctionLengthSniff:
maxLength: 20
ObjectCalisthenics\Sniffs\Files\PropertyPerClassLimit:
ObjectCalisthenics\Sniffs\Metrics\PropertyPerClassLimitSniff:
maxCount: 10
ObjectCalisthenics\Sniffs\Metrics\MethodPerClassLimit:
ObjectCalisthenics\Sniffs\Metrics\MethodPerClassLimitSniff:
maxCount: 10

# Rule 9: Do not Use Getters and Setters
# disable for temp bug in Symplify\EasyCodingStandard
# - ObjectCalisthenics\Sniffs\Classes\ForbiddenPublicPropertySniff
- ObjectCalisthenics\Sniffs\Classes\ForbiddenPublicPropertySniff
- ObjectCalisthenics\Sniffs\NamingConventions\NoSetterSniff

# Classes
Expand Down Expand Up @@ -70,8 +69,7 @@ checkers:

- PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\FunctionCallArgumentSpacingSniff
- PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\ValidDefaultValueSniff
PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions\FunctionDeclarationSniff:
equalsSpacing: 1
- PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions\FunctionDeclarationSniff
- PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions\MultiLineFunctionDeclarationSniff
- PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions\LowercaseFunctionKeywordsSniff

Expand All @@ -97,22 +95,14 @@ checkers:
ignoreBlankLines: false

# Symplify: Commenting
- Symplify\CodingStandard\Sniffs\Commenting\BlockPropertyCommentSniff
- Symplify\CodingStandard\Sniffs\Commenting\MethodReturnTypeSniff
- Symplify\CodingStandard\Sniffs\Commenting\MethodCommentSniff
- Symplify\CodingStandard\Sniffs\Commenting\VarPropertyCommentSniff

# Symplify: Control Structures
- Symplify\CodingStandard\Sniffs\ControlStructures\NewClassSniff

# Symplify: Debug
- Symplify\CodingStandard\Sniffs\Debug\DebugFunctionCallSniff

# Namespaces
- Symplify\CodingStandard\Sniffs\Namespaces\ClassNamesWithoutPreSlashSniff

- PhpCsFixer\Fixer\NamespaceNotation\SingleBlankLineBeforeNamespaceFixer
- PhpCsFixer\Fixer\NamespaceNotation\BlankLineAfterNamespaceFixer

# Symplify: Naming
- Symplify\CodingStandard\Sniffs\Naming\AbstractClassNameSniff
Expand Down Expand Up @@ -177,7 +167,6 @@ checkers:
- PhpCsFixer\Fixer\ClassNotation\ClassDefinitionFixer
- PhpCsFixer\Fixer\ControlStructure\ElseifFixer
- PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer
- PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer
- PhpCsFixer\Fixer\Whitespace\IndentationTypeFixer
- PhpCsFixer\Fixer\Whitespace\LineEndingFixer
- PhpCsFixer\Fixer\Casing\LowercaseKeywordsFixer
Expand All @@ -188,8 +177,6 @@ checkers:
- PhpCsFixer\Fixer\Whitespace\NoTrailingWhitespaceFixer
- PhpCsFixer\Fixer\Comment\NoTrailingWhitespaceInCommentFixer
- PhpCsFixer\Fixer\Whitespace\SingleBlankLineAtEofFixer
PhpCsFixer\Fixer\ClassNotation\SingleClassElementPerStatementFixer:
- property
- PhpCsFixer\Fixer\Import\SingleImportPerStatementFixer
- PhpCsFixer\Fixer\Import\SingleLineAfterImportsFixer
- PhpCsFixer\Fixer\ControlStructure\SwitchCaseSemicolonToColonFixer
Expand All @@ -203,8 +190,6 @@ checkers:
PhpCsFixer\Fixer\Basic\BracesFixer:
allow_single_line_closure: true
- PhpCsFixer\Fixer\CastNotation\CastSpacesFixer
PhpCsFixer\Fixer\ClassNotation\ClassDefinitionFixer:
singleLine: true
- PhpCsFixer\Fixer\LanguageConstruct\DeclareEqualNormalizeFixer
- PhpCsFixer\Fixer\FunctionNotation\FunctionTypehintSpaceFixer
- PhpCsFixer\Fixer\Comment\HashToSlashCommentFixer
Expand Down Expand Up @@ -246,7 +231,6 @@ checkers:
- PhpCsFixer\Fixer\Phpdoc\PhpdocNoAccessFixer
- PhpCsFixer\Fixer\Phpdoc\PhpdocNoEmptyReturnFixer
- PhpCsFixer\Fixer\Phpdoc\PhpdocNoPackageFixer
- PhpCsFixer\Fixer\Phpdoc\PhpdocNoUselessInheritdocFixer
- PhpCsFixer\Fixer\Phpdoc\PhpdocReturnSelfReferenceFixer
- PhpCsFixer\Fixer\Phpdoc\PhpdocScalarFixer
- PhpCsFixer\Fixer\Phpdoc\PhpdocSingleLineVarSpacingFixer
Expand All @@ -259,7 +243,6 @@ checkers:
- PhpCsFixer\Fixer\FunctionNotation\ReturnTypeDeclarationFixer
- PhpCsFixer\Fixer\ClassNotation\SelfAccessorFixer
- PhpCsFixer\Fixer\CastNotation\ShortScalarCastFixer
- PhpCsFixer\Fixer\NamespaceNotation\SingleBlankLineBeforeNamespaceFixer
- PhpCsFixer\Fixer\ClassNotation\SingleClassElementPerStatementFixer
- PhpCsFixer\Fixer\StringNotation\SingleQuoteFixer
- PhpCsFixer\Fixer\Semicolon\SpaceAfterSemicolonFixer
Expand All @@ -279,17 +262,12 @@ checkers:
- method

# new since PhpCsFixer 2.1/2.2
- PhpCsFixer\Fixer\Phpdoc\PhpdocNoUselessInheritdocFixer
- PhpCsFixer\Fixer\Phpdoc\PhpdocReturnSelfReferenceFixer
- PhpCsFixer\Fixer\LanguageConstruct\IsNullFixer
- PhpCsFixer\Fixer\LanguageConstruct\FunctionToConstantFixer
- PhpCsFixer\Fixer\Basic\NonPrintableCharacterFixer
- PhpCsFixer\Fixer\Casing\MagicConstantCasingFixer

skip:
# bugged in Symplify, to be fixed soon
SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff:
- src/ObjetCalisthenics/Sniffs/*/*Sniff.php
# bugged in Symplify, to be fixed soon
ObjectCalisthenics\Sniffs\Classes\ForbiddenPublicPropertySniff:
- src/ObjetCalisthenics/Sniffs/*/*Sniff.php
parameters:
skip:
SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff:
- src/ObjectCalisthenics/Sniffs/*/*Sniff.php
Expand Up @@ -6,5 +6,4 @@

final class NonClassTypeTokenTypeException extends Exception
{

}
Expand Up @@ -46,7 +46,7 @@ public function process(File $file, $position): void

private function isSniffClass(File $file, int $position): bool
{
$classTokenPosition = $file->findPrevious(T_CLASS, $position);
$classTokenPosition = (int) $file->findPrevious(T_CLASS, $position);
$classNameTokenPosition = $file->findNext(T_STRING, $classTokenPosition);

$classNameToken = $file->getTokens()[$classNameTokenPosition];
Expand All @@ -63,6 +63,7 @@ private function isSniffClass(File $file, int $position): bool
private function getPropertyScopeModifier(File $file, int $position): array
{
$scopeModifierPosition = $file->findPrevious(Tokens::$scopeModifiers, ($position - 1));

return $file->getTokens()[$scopeModifierPosition];
}
}
33 changes: 0 additions & 33 deletions tests/CodeSnifferRunner.php

This file was deleted.

52 changes: 41 additions & 11 deletions tests/Sniffs/AbstractSniffTestCase.php
Expand Up @@ -6,14 +6,35 @@
use Nette\Utils\Strings;
use PHPUnit\Framework\TestCase;
use SplFileInfo;
use Symplify\EasyCodingStandard\DependencyInjection\ContainerFactory;
use Symplify\EasyCodingStandard\Error\ErrorCollector;
use Symplify\EasyCodingStandard\SniffRunner\Application\SniffFileProcessor;
use Symplify\EasyCodingStandard\SniffRunner\Fixer\Fixer;

/**
* See https://github.com/Symplify/Symplify/blob/master/packages/CodingStandard/tests/Sniffs/AbstractSniffTestCase.php
*/
abstract class AbstractSniffTestCase extends TestCase
{
/**
* @var SniffFileProcessor
*/
private $sniffFileProcessor;

/**
* @var ErrorCollector
*/
private $errorCollector;

/**
* @var Fixer
*/
private $fixer;

protected function runSniffTestForDirectory(string $sniffClass, string $directory): void
{
$container = (new ContainerFactory)->createWithoutConfig();
$this->sniffFileProcessor = $container->get(SniffFileProcessor::class);
$this->errorCollector = $container->get(ErrorCollector::class);
$this->fixer = $container->get(Fixer::class);

foreach ($this->findFilesInDirectory($directory) as $file) {
if (Strings::startsWith($file->getFilename(), 'correct')) {
$this->runSniffTestForCorrectFile($sniffClass, $file);
Expand All @@ -27,18 +48,20 @@ protected function runSniffTestForDirectory(string $sniffClass, string $director

private function runSniffTestForCorrectFile(string $sniffClass, SplFileInfo $fileInfo): void
{
$errorCount = SniffRunner::getErrorCountForSniffInFile($sniffClass, $fileInfo);
$this->assertSame(0, $errorCount, sprintf(
$this->processFileWithSniff($sniffClass, $fileInfo);

$this->assertSame(0, $this->errorCollector->getErrorCount(), sprintf(
'File "%s" should have no errors. %s found.',
$fileInfo->getPathname(),
$errorCount
$this->errorCollector->getErrorCount()
));
}

private function runSniffTestForWrongFile(string $sniffClass, SplFileInfo $fileInfo): void
{
$errorCount = SniffRunner::getErrorCountForSniffInFile($sniffClass, $fileInfo);
$this->assertSame(1, $errorCount, sprintf(
$this->processFileWithSniff($sniffClass, $fileInfo);

$this->assertSame(1, $this->errorCollector->getErrorCount(), sprintf(
'File "%s" should have at least 1 error.',
$fileInfo->getPathname()
));
Expand All @@ -48,12 +71,11 @@ private function runSniffTestForWrongFile(string $sniffClass, SplFileInfo $fileI
return;
}

$fixedContent = SniffRunner::getFixedContentForSniffInFile($sniffClass, $fileInfo);
$this->assertStringEqualsFile($fixedFileName, $fixedContent, sprintf(
$this->assertStringEqualsFile($fixedFileName, $this->fixer->getContents(), sprintf(
'File "%s" was not fixed properly. "%s" expected, "%s" given.',
$fileInfo->getPathname(),
file_get_contents($fixedFileName),
$fixedContent
$this->fixer->getContents()
));
}

Expand All @@ -74,4 +96,12 @@ private function getFixedFileName(SplFileInfo $fileInfo): string
{
return dirname($fileInfo->getPathname()) . '/' . $fileInfo->getBasename('.php.inc') . '-fixed.php.inc';
}

private function processFileWithSniff(string $sniffClass, SplFileInfo $fileInfo): void
{
$this->errorCollector->resetCounters();
$this->sniffFileProcessor->setIsFixer(true); // to test changed content of file
$this->sniffFileProcessor->setSingleSniff(new $sniffClass);
$this->sniffFileProcessor->processFile($fileInfo, true);
}
}
6 changes: 6 additions & 0 deletions tests/Sniffs/Classes/correct/correct.php.inc
@@ -0,0 +1,6 @@
<?php

class Test
{
private $one, $two;
}
6 changes: 0 additions & 6 deletions tests/Sniffs/Classes/wrong/wrong.php.inc
Expand Up @@ -3,10 +3,4 @@
class Test
{
public $public;

protected $protected;

private $private;

private $one, $two;
}
24 changes: 3 additions & 21 deletions tests/Sniffs/CodeAnalysis/OneObjectOperatorPerLineSniffTest.php
Expand Up @@ -3,30 +3,12 @@
namespace ObjectCalisthenics\Tests\Sniffs\CodeAnalysis;

use ObjectCalisthenics\Sniffs\CodeAnalysis\OneObjectOperatorPerLineSniff;
use ObjectCalisthenics\Tests\CodeSnifferRunner;
use PHPUnit\Framework\TestCase;
use ObjectCalisthenics\Tests\Sniffs\AbstractSniffTestCase;

final class OneObjectOperatorPerLineSniffTest extends TestCase
final class OneObjectOperatorPerLineSniffTest extends AbstractSniffTestCase
{
public function test(): void
{
$codeSnifferRunner = new CodeSnifferRunner();
$errorCount = $codeSnifferRunner->detectErrorCountInFileForSniff(
__DIR__ . '/OneObjectOperatorPerLineSniffTest.inc',
OneObjectOperatorPerLineSniff::class
);

$this->assertSame(2, $errorCount);
}

public function testFluentInterfaces(): void
{
$codeSnifferRunner = new CodeSnifferRunner();
$errorCount = $codeSnifferRunner->detectErrorCountInFileForSniff(
__DIR__ . '/OneObjectOperatorPerLineSniffTestFluentInterface.inc',
OneObjectOperatorPerLineSniff::class
);

$this->assertSame(1, $errorCount);
$this->runSniffTestForDirectory(OneObjectOperatorPerLineSniff::class, __DIR__);
}
}
Expand Up @@ -29,40 +29,3 @@ class User
return $this->sessionStorage->name;
}
}

class MySessionStorage
{
public $name = 'mySessionStorage';

public function __construct()
{
$this->driver = new NativeSessionStorage();
}

public function start()
{
$this->driver->getInternal()->start();
}
}

class NativeSessionStorage
{
public function getInternal()
{
return new PHPSessionStorage();
}
}

class PHPSessionStorage
{
public function start()
{
session_start();

return $this;
}
}

$user = new User();

$user->sessionStorage->start();

0 comments on commit 2aaf984

Please sign in to comment.