Skip to content

Commit

Permalink
Merge dcebf7f into d49733c
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 6, 2020
2 parents d49733c + dcebf7f commit bfcf27e
Show file tree
Hide file tree
Showing 52 changed files with 204 additions and 248 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/code_analysis.yaml
@@ -0,0 +1,45 @@
name: Code Analysis

on:
pull_request: null
push:
branches:
- master

jobs:
matrix:
strategy:
fail-fast: false
matrix:
actions:
-
name: 'PHPStan'
run: composer phpstan

-
name: 'ECS'
run: composer check-cs

-
name: 'Rector'
run: composer rector

-
name: 'Code Sniffer Set Run'
run: 'vendor/bin/phpcs src tests -sp --standard=src/ObjectCalisthenics/ruleset.xml --extensions=php'

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
# see https://github.com/shivammathur/setup-php
- uses: shivammathur/setup-php@v1
with:
php-version: 7.4
coverage: none

- run: composer install --no-progress

-
name: ${{ matrix.actions.name }}
run: ${{ matrix.actions.run }}
20 changes: 0 additions & 20 deletions .github/workflows/code_sniffer_set_run.yaml

This file was deleted.

20 changes: 0 additions & 20 deletions .github/workflows/ecs.yaml

This file was deleted.

20 changes: 0 additions & 20 deletions .github/workflows/phpstan.yaml

This file was deleted.

20 changes: 0 additions & 20 deletions .github/workflows/rector.yaml

This file was deleted.

23 changes: 12 additions & 11 deletions composer.json
Expand Up @@ -6,18 +6,19 @@
"require": {
"php": "^7.2",
"nette/utils": "^3.1",
"slevomat/coding-standard": "^6.2",
"slevomat/coding-standard": "^6.3",
"squizlabs/php_codesniffer": "^3.5"
},
"require-dev": {
"phpstan/phpstan": "^0.12.18",
"phpstan/phpstan": "^0.12.32",
"phpunit/phpunit": "^8.5|^9.0",
"rector/rector": "^0.7.9",
"symplify/changelog-linker": "^7.2",
"symplify/coding-standard": "^7.2",
"symplify/easy-coding-standard-tester": "^7.2",
"symplify/phpstan-extensions": "^7.2",
"tracy/tracy": "^2.7"
"rector/rector": "^0.7.43",
"symplify/changelog-linker": "^8.1",
"symplify/coding-standard": "^8.1",
"symplify/easy-coding-standard-tester": "^8.1",
"symplify/phpstan-extensions": "^8.1",
"tracy/tracy": "^2.7",
"phpstan/phpdoc-parser": "0.4.8 as 0.4.4"
},
"autoload": {
"psr-4": {
Expand All @@ -31,9 +32,9 @@
},
"scripts": {
"complete-check": ["@phpstan", "@check-cs", "phpunit"],
"phpstan": "phpstan analyze src tests --error-format symplify",
"check-cs": "ecs check",
"fix-cs": "ecs check --fix",
"phpstan": "phpstan analyze src tests --error-format symplify --ansi",
"check-cs": "ecs check --ansi",
"fix-cs": "ecs check --fix --ansi",
"changelog": "changelog-linker dump-merges",
"rector": "rector process --dry-run --config rector-ci.yaml --ansi"
}
Expand Down
3 changes: 3 additions & 0 deletions ecs.yaml
Expand Up @@ -44,3 +44,6 @@ parameters:
# mutually excluded
SlevomatCodingStandard\Sniffs\ControlStructures\DisallowYodaComparisonSniff: ~
PhpCsFixer\Fixer\Operator\UnaryOperatorSpacesFixer: ~

ObjectCalisthenics\Sniffs\Files\ClassTraitAndInterfaceLengthSniff:
- 'src/ObjectCalisthenics/Sniffs/CodeAnalysis/OneObjectOperatorPerLineSniff.php'
2 changes: 1 addition & 1 deletion phpstan.neon
Expand Up @@ -10,7 +10,7 @@ parameters:
- src
- tests

autoload_files:
bootstrapFiles:
- 'tests/bootstrap.php'

ignoreErrors:
Expand Down
Expand Up @@ -12,6 +12,36 @@ final class OneObjectOperatorPerLineSniff implements Sniff
*/
private const ERROR_MESSAGE = 'Only one object operator per line.';

/**
* @var string
*/
private const CONTENT = 'content';

/**
* @var string
*/
private const CODE = 'code';

/**
* @var string
*/
private const TOKEN = 'token';

/**
* @var string
*/
private const TYPE = 'type';

/**
* @var string
*/
private const METHOD = 'method';

/**
* @var string
*/
private const PROPERTY = 'property';

/**
* @var string[]
*/
Expand Down Expand Up @@ -72,10 +102,10 @@ public function process(File $file, $position): void
$this->callerTokens = [];

$pointer = $this->ignoreWhitespace($position + 1);
$this->variableName = $this->tokens[$this->position]['content'];
$this->variableName = $this->tokens[$this->position][self::CONTENT];

$token = $this->tokens[$position];
$isOwnCall = ($token['content'] === '$this');
$isOwnCall = ($token[self::CONTENT] === '$this');

$this->handleObjectOperators($pointer, $isOwnCall);
}
Expand All @@ -84,7 +114,7 @@ private function ignoreWhitespace(int $start): int
{
$pointer = $start;

while ($this->tokens[$pointer]['code'] === T_WHITESPACE) {
while ($this->tokens[$pointer][self::CODE] === T_WHITESPACE) {
++$pointer;
}

Expand All @@ -93,7 +123,7 @@ private function ignoreWhitespace(int $start): int

private function handleObjectOperators(int $pointer, bool $isOwnCall): void
{
while ($this->tokens[$pointer]['code'] === T_OBJECT_OPERATOR) {
while ($this->tokens[$pointer][self::CODE] === T_OBJECT_OPERATOR) {
$tmpToken = $this->tokens[++$pointer];
$pointer = $this->ignoreWhitespace($pointer + 1);
$tmpTokenType = $this->getTokenType($this->tokens[$pointer]);
Expand All @@ -103,8 +133,8 @@ private function handleObjectOperators(int $pointer, bool $isOwnCall): void
$this->handleExcludedFluentInterfaces($tmpToken, $tmpTokenType, $isOwnCall);

$this->callerTokens[] = [
'token' => $tmpToken,
'type' => $tmpTokenType,
self::TOKEN => $tmpToken,
self::TYPE => $tmpTokenType,
];

$pointer = $this->movePointerToNextObject($pointer);
Expand All @@ -116,11 +146,11 @@ private function handleObjectOperators(int $pointer, bool $isOwnCall): void
*/
private function getTokenType(array $token): string
{
if ($token['code'] === T_OPEN_PARENTHESIS) {
return 'method';
if ($token[self::CODE] === T_OPEN_PARENTHESIS) {
return self::METHOD;
}

return 'property';
return self::PROPERTY;
}

private function handleTwoObjectOperators(bool $isOwnCall): void
Expand All @@ -145,10 +175,10 @@ private function handleExcludedFluentInterfaces(array $tmpToken, string $tmpToke
return;
}

if (($memberToken['type'] === 'property' && $tmpTokenType === 'property')
|| ($memberToken['type'] === 'method' && $tmpTokenType === 'property')
|| ($memberToken['type'] === 'method' && $tmpTokenType === 'method'
&& $memberTokenCount > 1 && $tmpToken['content'] !== $memberToken['token']['content']
if (($memberToken[self::TYPE] === self::PROPERTY && $tmpTokenType === self::PROPERTY)
|| ($memberToken[self::TYPE] === self::METHOD && $tmpTokenType === self::PROPERTY)
|| ($memberToken[self::TYPE] === self::METHOD && $tmpTokenType === self::METHOD
&& $memberTokenCount > 1 && $tmpToken[self::CONTENT] !== $memberToken[self::TOKEN][self::CONTENT]
&& ! $this->isInFluentInterfaceMode())
) {
$this->file->addError(self::ERROR_MESSAGE, $this->position, self::class);
Expand All @@ -160,7 +190,7 @@ private function movePointerToNextObject(int $pointer): int
$token = $this->tokens[$pointer];

// Ignore "(" ... ")" in a method call by moving pointer after close parenthesis token
if ($token['code'] === T_OPEN_PARENTHESIS) {
if ($token[self::CODE] === T_OPEN_PARENTHESIS) {
$pointer = $token['parenthesis_closer'] + 1;
}

Expand Down Expand Up @@ -189,7 +219,7 @@ private function isInFluentInterfaceMode(): bool
private function computeLastCallOfAnyFrom(array $methods): int
{
$calls = array_filter($this->callerTokens, function (array $token) use ($methods): bool {
return in_array($token['token']['content'], $methods, true);
return in_array($token[self::TOKEN][self::CONTENT], $methods, true);
});
if (count($calls) > 0) {
return (int) array_search(end($calls), $this->callerTokens, true);
Expand Down
11 changes: 8 additions & 3 deletions src/ObjectCalisthenics/Sniffs/Metrics/MaxNestingLevelSniff.php
Expand Up @@ -12,6 +12,11 @@ final class MaxNestingLevelSniff implements Sniff
*/
private const ERROR_MESSAGE = 'Only %d indentation level%s per function/method. Found %s levels.';

/**
* @var string
*/
private const SCOPE_CLOSER = 'scope_closer';

/**
* @var int
*/
Expand Down Expand Up @@ -68,7 +73,7 @@ public function process(File $file, $position): void
return;
}

$this->iterateTokens($token['scope_opener'], $token['scope_closer'], $tokens);
$this->iterateTokens($token['scope_opener'], $token[self::SCOPE_CLOSER], $tokens);

$this->nestingLevel = $this->subtractFunctionNestingLevel($token);
$this->handleNestingLevel($this->nestingLevel);
Expand Down Expand Up @@ -132,7 +137,7 @@ private function handleClosureToken(array $nestedToken): void
if ($nestedToken['code'] === T_CLOSURE) {
// Move index pointer in case we found a lambda function
// (another call process will deal with its check later).
$this->currentPtr = $nestedToken['scope_closer'];
$this->currentPtr = $nestedToken[self::SCOPE_CLOSER];

return;
}
Expand Down Expand Up @@ -164,7 +169,7 @@ private function adjustNestingLevelToIgnoredScope(): void
*/
private function unsetScopeIfNotCurrent(int $key, array $ignoredScope): void
{
if ($ignoredScope['scope_closer'] !== $this->currentPtr) {
if ($ignoredScope[self::SCOPE_CLOSER] !== $this->currentPtr) {
return;
}

Expand Down
File renamed without changes.
18 changes: 9 additions & 9 deletions tests/Sniffs/Classes/ForbiddenPublicPropertySniffTest.php
Expand Up @@ -5,18 +5,18 @@
use Iterator;
use ObjectCalisthenics\Sniffs\Classes\ForbiddenPublicPropertySniff;
use Symplify\EasyCodingStandardTester\Testing\AbstractCheckerTestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\SmartFileSystem\SmartFileInfo;

/**
* @see ForbiddenPublicPropertySniff
*/
final class ForbiddenPublicPropertySniffTest extends AbstractCheckerTestCase
{
/**
* @dataProvider provideCorrectCases()
*/
public function testCorrectCases(string $file): void
{
$this->doTestCorrectFile($file);
$fileInfo = new SmartFileInfo($file);
$this->doTestFileInfo($fileInfo);
}

public function provideCorrectCases(): Iterator
Expand All @@ -27,18 +27,18 @@ public function provideCorrectCases(): Iterator
/**
* @dataProvider provideWrongCases()
*/
public function testWrongToFixed(string $wrongFile): void
public function testWrongToFixed(SmartFileInfo $wrongFile): void
{
$this->doTestWrongFile($wrongFile);
$this->doTestFileInfoWithErrorCountOf($wrongFile, 1);
}

public function provideWrongCases(): Iterator
{
yield [__DIR__ . '/wrong/wrong.php.inc'];
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture');
}

protected function provideConfig(): string
protected function getCheckerClass(): string
{
return __DIR__ . '/config.yml';
return ForbiddenPublicPropertySniff::class;
}
}

0 comments on commit bfcf27e

Please sign in to comment.