Skip to content

Commit

Permalink
Merge pull request #62 from object-calisthenics/codesniffer-3
Browse files Browse the repository at this point in the history
Codesniffer 3
  • Loading branch information
Tomáš Votruba committed May 30, 2017
2 parents 413300f + 194d3e6 commit ad32bdd
Show file tree
Hide file tree
Showing 19 changed files with 341 additions and 73 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -19,6 +19,8 @@ script:
- if [[ $CHECK_CS = 1 ]]; then composer check-cs; fi
# check with phpstan (defined in composer.json "scripts" section)
- if [[ $RUN_PHPSTAN = 1 ]]; then composer phpstan; fi
# run PHP_CodeSniffer with ruleset
- vendor/bin/phpcs src tests -sp --standard=src/ObjectCalisthenics/ruleset.xml --extensions=php --colors --ignore="Slevomat,SniffRunner"

after_script:
# upload coverage.xml file to Scrutinizer to analyze it
Expand Down
61 changes: 61 additions & 0 deletions README.md
Expand Up @@ -195,6 +195,67 @@ class SimpleStartupController
}
```

:x:

```php
class SomeClass
{
public function simpleLogic()
{
// 30 lines of code
}
}
```

:+1:

```php
class SomeClass
{
public function simpleLogic()
{
// 10 lines of code
}
}
```

:x:

```php
class SomeClass
{
// 20 properties
}
```

:+1:

```php
class SomeClass
{
// 5 properties
}
```

:x:

```php
class SomeClass
{
// 20 methods
}
```

:+1:

```php
class SomeClass
{
// 5 methods
}
```


#### Apply in CLI?

```bash
Expand Down
12 changes: 5 additions & 7 deletions composer.json
Expand Up @@ -5,16 +5,15 @@
"type": "phpcodesniffer-standard",
"require": {
"php": "^7.1",
"squizlabs/php_codesniffer": "3.0.0RC4 as 2.8.1",
"nette/utils": "^2.4",
"slevomat/coding-standard": "^2.0"
"squizlabs/php_codesniffer": "dev-master",
"nette/utils": "^2.4"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"friendsofphp/php-cs-fixer": "^2.2",
"phpstan/phpstan": "^0.6",
"tracy/tracy": "^2.4",
"symplify/easy-coding-standard": "^1.4"
"symplify/easy-coding-standard": "dev-master",
"slevomat/coding-standard": "dev-phpcs3"
},
"autoload": {
"psr-4": {
Expand All @@ -23,8 +22,7 @@
},
"autoload-dev": {
"psr-4": {
"ObjectCalisthenics\\Tests\\": "tests",
"PHP_CodeSniffer\\": "vendor/squizlabs/php_codesniffer/src"
"ObjectCalisthenics\\Tests\\": "tests"
}
},
"scripts": {
Expand Down
20 changes: 10 additions & 10 deletions easy-coding-standard.neon
Expand Up @@ -21,13 +21,13 @@ checkers:

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

# Rule 9: Do not Use Getters and Setters
# disable for temp bug in Symplify\EasyCodingStandard
Expand Down Expand Up @@ -132,11 +132,11 @@ checkers:
- SlevomatCodingStandard\Sniffs\TypeHints\NullableTypeForNullDefaultValueSniff
- SlevomatCodingStandard\Sniffs\TypeHints\ReturnTypeHintSpacingSniff
# disable for temp bug in Symplify\EasyCodingStandard
# SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff:
# usefulAnnotations:
# - @todo
# - @dataProvider
# - @expectedException
SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff:
usefulAnnotations:
- @todo
- @dataProvider
- @expectedException
- SlevomatCodingStandard\Sniffs\Namespaces\AlphabeticallySortedUsesSniff
- SlevomatCodingStandard\Sniffs\Namespaces\DisallowGroupUseSniff
- SlevomatCodingStandard\Sniffs\Namespaces\MultipleUsesPerLineSniff
Expand Down
6 changes: 4 additions & 2 deletions phpstan.neon
@@ -1,3 +1,5 @@
parameters:
ignoreErrors:
- "#expects PHP_CodeSniffer_File#"
autoload_files:
- %currentWorkingDirectory%/vendor/squizlabs/php_codesniffer/autoload.php
excludes_analyse:
- **/ObjectCalisthenics/Helper/Slevomat/**
11 changes: 5 additions & 6 deletions phpunit.xml
@@ -1,13 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
colors="true"
verbose="true"
>
<testsuites>
<testsuite>
<directory>tests</directory>
</testsuite>
</testsuites>
<testsuite>
<directory>tests</directory>
</testsuite>
<filter>
<whitelist>
<directory>src</directory>
Expand Down
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace ObjectCalisthenics\Exception\Helper;

use Exception;

final class NonClassTypeTokenTypeException extends Exception
{

}
7 changes: 3 additions & 4 deletions src/ObjectCalisthenics/Helper/ClassAnalyzer.php
Expand Up @@ -2,7 +2,7 @@

namespace ObjectCalisthenics\Helper;

use Exception;
use ObjectCalisthenics\Exception\Helper\NonClassTypeTokenTypeException;
use PHP_CodeSniffer\Files\File;
use SlevomatCodingStandard\Helpers\PropertyHelper;

Expand Down Expand Up @@ -54,7 +54,6 @@ public static function getClassProperties(File $file, int $position): array

private static function extractPropertyIfFound(File $file, int $position): void
{
LegacyCompatibilityLayer::setupClassAliases();
if (PropertyHelper::isProperty($file, $position)) {
self::$propertyList[] = $position;
}
Expand All @@ -64,8 +63,8 @@ private static function ensureIsClassTraitOrInterface(File $file, int $position)
{
$token = $file->getTokens()[$position];

if (! in_array($token['code'], [T_CLASS, T_INTERFACE, T_TRAIT])) {
throw new Exception(
if (! in_array($token['code'], [T_CLASS, T_INTERFACE, T_TRAIT], true)) {
throw new NonClassTypeTokenTypeException(
sprintf(
'Must be class, interface or trait. "%s" given.',
ltrim($token['type'], 'T_')
Expand Down
15 changes: 0 additions & 15 deletions src/ObjectCalisthenics/Helper/LegacyCompatibilityLayer.php

This file was deleted.

2 changes: 0 additions & 2 deletions src/ObjectCalisthenics/Helper/Naming.php
Expand Up @@ -26,8 +26,6 @@ public static function getTypeName(File $file, int $position): string
return self::$codeToTypeNameMap[$tokenCode];
}

LegacyCompatibilityLayer::setupClassAliases();

if ($token['code'] === T_VARIABLE) {
if (PropertyHelper::isProperty($file, $position)) {
return 'Property';
Expand Down
Expand Up @@ -2,7 +2,7 @@

namespace ObjectCalisthenics\Sniffs\Classes;

use ObjectCalisthenics\Helper\LegacyCompatibilityLayer;
use Nette\Utils\Strings;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
Expand All @@ -29,16 +29,40 @@ public function register(): array
*/
public function process(File $file, $position): void
{
LegacyCompatibilityLayer::setupClassAliases();
if (! PropertyHelper::isProperty($file, $position)) {
return;
}

$scopeModifier = $file->findPrevious(Tokens::$scopeModifiers, ($position - 1));
// skip Sniff classes, they have public properties for configuration (unfortunately)
if ($this->isSniffClass($file, $position)) {
return;
}

$tokens = $file->getTokens();
if ($tokens[$scopeModifier]['code'] === T_PUBLIC) {
$scopeModifierToken = $this->getPropertyScopeModifier($file, $position);
if ($scopeModifierToken['code'] === T_PUBLIC) {
$file->addError(self::ERROR_MESSAGE, $position, self::class);
}
}

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

$classNameToken = $file->getTokens()[$classNameTokenPosition];
if (Strings::endsWith($classNameToken['content'], 'Sniff')) {
return true;
}

return false;
}

/**
* @return mixed[]
*/
private function getPropertyScopeModifier(File $file, int $position): array
{
$scopeModifierPosition = $file->findPrevious(Tokens::$scopeModifiers, ($position - 1));
return $file->getTokens()[$scopeModifierPosition];
}
}
12 changes: 6 additions & 6 deletions src/ObjectCalisthenics/ruleset.xml
Expand Up @@ -30,22 +30,22 @@
<!-- Rule 7: Keep your classes small (with default values) -->
<rule ref="ObjectCalisthenics.Files.ClassTraitAndInterfaceLength">
<properties>
<property name="maxCount" value="200"/>
<property name="maxLength" value="200"/>
</properties>
</rule>
<rule ref="ObjectCalisthenics.Metrics.MethodPerClassLimit">
<rule ref="ObjectCalisthenics.Files.FunctionLength">
<properties>
<property name="maxCount" value="10"/>
<property name="maxLength" value="20"/>
</properties>
</rule>
<rule ref="ObjectCalisthenics.Metrics.PropertyPerClassLimit">
<properties>
<property name="maxLength" value="10"/>
<property name="maxCount" value="10"/>
</properties>
</rule>
<rule ref="ObjectCalisthenics.Files.FunctionLength">
<rule ref="ObjectCalisthenics.Metrics.MethodPerClassLimit">
<properties>
<property name="maxLength" value="20"/>
<property name="maxCount" value="10"/>
</properties>
</rule>

Expand Down
2 changes: 1 addition & 1 deletion tests/CodeSnifferRunner.php
Expand Up @@ -13,7 +13,7 @@ final class CodeSnifferRunner

public function __construct()
{
$this->fileFactory = new FileFactory();
$this->fileFactory = new FileFactory;
}

public function detectErrorCountInFileForSniff(string $testedFile, string $sniffName): int
Expand Down
10 changes: 5 additions & 5 deletions tests/FileFactory.php
Expand Up @@ -50,11 +50,11 @@ public function createFileWithSniffClass(string $filePath, string $sniffClass):

private function createRulesetWithConfigAndSniffClass(string $sniffClass, Config $config): Ruleset
{
$config->sniffs = [$sniffClass];
$config->standards = ['ObjectCalisthenics'];

$ruleset = new Ruleset($config);
$ruleset->sniffs = [
$sniffClass => new $sniffClass(),
];
$ruleset->populateTokenListeners();
// $ruleset->populateTokenListeners();

return $ruleset;
}
Expand All @@ -63,7 +63,7 @@ private function createConfig(): Config
{
$config = new Config();
// nulling required, because PEAR Standard is on by default
$config->standards = [];
// $config->standards = [];

return $config;
}
Expand Down

0 comments on commit ad32bdd

Please sign in to comment.