Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/PhpCs/FiveLab/ErrorCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ final class ErrorCodes
public const NAMESPACE_WRONG = 'NamespaceWrong';
public const ARRAYS_DOC_VECTOR = 'ArraysDocVector';
public const PHPDOC_NOT_ALLOWED = 'PhpDocNotAllowed';
public const MISSED_SPACE = 'MissedSpace';
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function process(File $phpcsFile, mixed $stackPtr): void

$possiblyDeclaration = $phpcsFile->findNext([
T_CLASS, T_INTERFACE, T_TRAIT, T_FUNCTION,
T_ABSTRACT, T_FINAL, T_ENUM
T_ABSTRACT, T_FINAL, T_ENUM,
], $commentToken['comment_closer'] + 1, local: true);

if ($possiblyDeclaration) {
Expand Down
123 changes: 123 additions & 0 deletions src/PhpCs/FiveLab/Sniffs/Formatting/MissedSpacesSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

declare(strict_types = 1);

/*
* This file is part of the FiveLab CiRules package
*
* (c) FiveLab
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/

namespace FiveLab\Component\CiRules\PhpCs\FiveLab\Sniffs\Formatting;

use FiveLab\Component\CiRules\PhpCs\FiveLab\ErrorCodes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

class MissedSpacesSniff implements Sniff
{
public function register(): array
{
return [
T_BOOLEAN_AND,
T_BOOLEAN_OR,
T_INLINE_THEN,
T_INLINE_ELSE,
];
}

public function process(File $phpcsFile, mixed $stackPtr): void
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];

if ($tokens[$stackPtr - 1]['code'] !== T_WHITESPACE) {
if ($token['code'] === T_INLINE_ELSE || $token['code'] === T_INLINE_THEN) {
$this->analiseThenElse($phpcsFile, $stackPtr, true);

return;
}

$phpcsFile->addError(
'Missed one space before logical operand.',
$stackPtr,
ErrorCodes::MISSED_SPACE
);
}

if ($tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
if ($token['code'] === T_INLINE_THEN || $token['code'] === T_INLINE_ELSE) {
$this->analiseThenElse($phpcsFile, $stackPtr, false);

return;
}

$phpcsFile->addError(
'Missed one space after logical operand.',
$stackPtr,
ErrorCodes::MISSED_SPACE
);
}
}

private function analiseThenElse(File $phpcsFile, mixed $stackPtr, bool $before): void
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];

if ($token['code'] === T_INLINE_THEN) {
if ($before) {
$phpcsFile->addError(
'Missed one space before logical operand.',
$stackPtr,
ErrorCodes::MISSED_SPACE
);

return;
}

if ($tokens[$stackPtr + 1]['code'] === T_INLINE_ELSE) {
return;
}

$phpcsFile->addError(
'Missed one space after logical operand.',
$stackPtr,
ErrorCodes::MISSED_SPACE
);
}

if ($token['code'] === T_INLINE_ELSE) {
if ($before) {
if ($tokens[$stackPtr - 1]['code'] === T_INLINE_THEN) {
if ($tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
$phpcsFile->addError(
'Missed one space after logical operand.',
$stackPtr,
ErrorCodes::MISSED_SPACE
);
}

return;
}

$phpcsFile->addError(
'Missed one space before logical operand.',
$stackPtr,
ErrorCodes::MISSED_SPACE
);

return;
}

$phpcsFile->addError(
'Missed one space after logical operand.',
$stackPtr,
ErrorCodes::MISSED_SPACE
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ protected function processFunctionCall(File $phpcsFile, int $stackPtr, array $pa
}
}

/**
* Check before multiline call
*
* @param File $phpcsFile
* @param int $stackPtr
*/
private function checkBeforeCall(File $phpcsFile, int $stackPtr): void
{
$tokens = $phpcsFile->getTokens();
Expand Down Expand Up @@ -101,12 +95,6 @@ private function checkBeforeCall(File $phpcsFile, int $stackPtr): void
}
}

/**
* Check after multiline call
*
* @param File $phpcsFile
* @param int $closeParenthesisPtr
*/
private function checkAfterCall(File $phpcsFile, int $closeParenthesisPtr): void
{
$tokens = $phpcsFile->getTokens();
Expand All @@ -128,7 +116,7 @@ private function checkAfterCall(File $phpcsFile, int $closeParenthesisPtr): void

$possibleNextTokens = [T_CLOSE_CURLY_BRACKET, T_BREAK];

if (!\in_array($nextToken['code'], $possibleNextTokens, true) && $diffLines < 2) {
if ($diffLines < 2 && !\in_array($nextToken['code'], $possibleNextTokens, true)) {
$phpcsFile->addError(
'Must be one blank line after multiline call.',
$nextTokenPtr,
Expand Down
1 change: 1 addition & 0 deletions src/PhpCs/FiveLab/Sniffs/Namespace/NamespaceSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ private function getExpectedNamespace(File $phpcsFile): ?string
* @param string $composerJsonPath
*
* @return array<mixed>
*
* @throws \JsonException
*/
private function loadComposerJson(string $composerJsonPath): array
Expand Down
3 changes: 0 additions & 3 deletions src/PhpCs/FiveLab/Sniffs/Whitespace/ManyWhitespacesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

/**
* Check many whitespaces.
*/
class ManyWhitespacesSniff implements Sniff
{
public function register(): array
Expand Down
3 changes: 3 additions & 0 deletions src/PhpStan/MethodCallConsistencyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function processNode(Node $node, Scope $scope): array
* @param Scope $scope
*
* @return list<IdentifierRuleError>
*
* @throws ShouldNotHappenException
*/
private function checkStaticCall(Node\Expr\StaticCall $node, Scope $scope): array
Expand Down Expand Up @@ -96,6 +97,7 @@ private function checkStaticCall(Node\Expr\StaticCall $node, Scope $scope): arra
* @param Scope $scope
*
* @return list<IdentifierRuleError>
*
* @throws ShouldNotHappenException
*/
private function checkInstanceCall(Node\Expr\MethodCall $node, Scope $scope): array
Expand Down Expand Up @@ -130,6 +132,7 @@ private function checkInstanceCall(Node\Expr\MethodCall $node, Scope $scope): ar
* @param Scope $scope
*
* @return list<IdentifierRuleError>
*
* @throws ShouldNotHappenException|MissingMethodFromReflectionException
*/
private function checkNativeMethodCall(Node\Expr\StaticCall $node, Scope $scope): array
Expand Down
129 changes: 129 additions & 0 deletions tests/PhpCs/FiveLab/Sniffs/Formatting/MissedSpacesSniffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

declare(strict_types = 1);

/*
* This file is part of the FiveLab CiRules package
*
* (c) FiveLab
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/

namespace FiveLab\Component\CiRules\Tests\PhpCs\FiveLab\Sniffs\Formatting;

use FiveLab\Component\CiRules\PhpCs\FiveLab\Sniffs\Formatting\MissedSpacesSniff;
use FiveLab\Component\CiRules\Tests\PhpCs\FiveLab\Sniffs\SniffTestCase;

class MissedSpacesSniffTest extends SniffTestCase
{
protected function getSniffClass(): string
{
return MissedSpacesSniff::class;
}

public static function provideDataSet(): array
{
return [
'success' => [
__DIR__.'/Resources/missed-spaces/success.php',
],

'missed' => [
__DIR__.'/Resources/missed-spaces/wrong.php',
[
'message' => 'Missed one space before logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 7,
],
[
'message' => 'Missed one space after logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 7,
],
[
'message' => 'Missed one space after logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 8,
],
[
'message' => 'Missed one space before logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 9,
],
[
'message' => 'Missed one space before logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 11,
],
[
'message' => 'Missed one space after logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 11,
],
[
'message' => 'Missed one space after logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 12,
],
[
'message' => 'Missed one space before logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 13,
],
[
'message' => 'Missed one space before logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 15,
],
[
'message' => 'Missed one space after logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 15,
],
[
'message' => 'Missed one space before logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 16,
],
[
'message' => 'Missed one space before logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 16,
],
[
'message' => 'Missed one space after logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 18,
],
[
'message' => 'Missed one space before logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 19,
],
[
'message' => 'Missed one space after logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 21,
],
[
'message' => 'Missed one space before logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 21,
],
[
'message' => 'Missed one space before logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 22,
],
[
'message' => 'Missed one space after logical operand.',
'source' => 'FiveLab.Formatting.MissedSpaces.MissedSpace',
'line' => 23,
],
],

];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$a = 1;
$b = 2;
$c = 3;

if ($a === 3 || $b === 3) {}

if ($a === 3 && $b === 3) {}

$a = $b ?: $c;
$b = $a ? true : $c;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$a = 1;
$b = 2;
$c = 3;

if ($a === 3||$b === 3) {}
if ($a === 3 ||$b === 3) {}
if ($a === 3|| $b === 3) {}

if ($a === 3&&$b === 3) {}
if ($a === 3 &&$b === 3) {}
if ($a === 3&& $b === 3) {}

$a = $b?:$c;
$b = $a?$b:$c;

$a = $b ?:$c;
$a = $b?: $c;

$b = $a ?$b:$c;
$b = $a ? $b: $c;
$b = $a ? $b :$c;