Skip to content

Commit 2166911

Browse files
committed
added github workflows, fixed code style of sniffs
1 parent 4cd153d commit 2166911

File tree

7 files changed

+323
-337
lines changed

7 files changed

+323
-337
lines changed

.github/workflows/php.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: PHP
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
codestyle:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
16+
- name: Setup PHP
17+
uses: shivammathur/setup-php@v2
18+
with:
19+
php-version: '7.4'
20+
env:
21+
update: true
22+
23+
- name: Validate composer.json and composer.lock
24+
run: composer validate
25+
26+
- name: Install dependencies
27+
run: composer install --prefer-dist --no-progress --no-suggest
28+
29+
- name: Codestyle
30+
run: composer run-script codestyle
Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
<?php
22

3-
declare(strict_types=1);
3+
declare(strict_types = 1);
44

55
namespace InfinityloopCodingStandard\Sniffs\Classes;
66

7-
use PHP_CodeSniffer\Files\File;
8-
use PHP_CodeSniffer\Sniffs\Sniff;
9-
use PHP_CodeSniffer\Util\Tokens;
10-
use RuntimeException;
11-
use SlevomatCodingStandard\Helpers\ClassHelper;
12-
use SlevomatCodingStandard\Helpers\DocCommentHelper;
13-
use SlevomatCodingStandard\Helpers\FunctionHelper;
14-
use SlevomatCodingStandard\Helpers\TokenHelper;
15-
use const T_FINAL;
16-
use const T_CLASS;
17-
use const T_VARIABLE;
7+
use \SlevomatCodingStandard\Helpers\TokenHelper;
188

199
/**
2010
* This sniff prohibits the use of protected variables and functions inside final class
2111
*/
22-
class FinalClassVisibilitySniff implements Sniff
12+
class FinalClassVisibilitySniff implements \PHP_CodeSniffer\Sniffs\Sniff
2313
{
2414
/**
2515
* @var bool
@@ -29,61 +19,70 @@ class FinalClassVisibilitySniff implements Sniff
2919
public function register() : array
3020
{
3121
return [
32-
T_VARIABLE,
33-
T_FUNCTION,
22+
\T_VARIABLE,
23+
\T_FUNCTION,
3424
];
3525
}
3626

37-
public function process(File $phpcsFile, $variablePointer) : void
27+
public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $variablePointer) : void
3828
{
3929
$tokens = $phpcsFile->getTokens();
40-
if (count($tokens[$variablePointer]['conditions']) === 0) {
30+
31+
if (\count($tokens[$variablePointer]['conditions']) === 0) {
4132
return;
4233
}
43-
/** @var int $classPointer */
44-
$classPointer = array_keys($tokens[$variablePointer]['conditions'])[count($tokens[$variablePointer]['conditions']) - 1];
45-
if ($tokens[$classPointer]['code'] !== T_CLASS) {
34+
35+
$classPointer = \array_keys($tokens[$variablePointer]['conditions'])[\count($tokens[$variablePointer]['conditions']) - 1];
36+
\assert(\is_int($classPointer));
37+
38+
if ($tokens[$classPointer]['code'] !== \T_CLASS) {
4639
return;
4740
}
4841

4942
$classVisibilityPointer = TokenHelper::findPreviousEffective($phpcsFile, $classPointer - 1);
50-
if ($tokens[$classVisibilityPointer]['code'] !== T_FINAL) {
43+
44+
if ($tokens[$classVisibilityPointer]['code'] !== \T_FINAL) {
5145
return;
5246
}
5347

54-
$extendsPointer = $phpcsFile->findNext(T_EXTENDS, $classPointer);
55-
if($this->ignoreInheritingClasses && $tokens[$extendsPointer]['code'] === T_EXTENDS) {
48+
$extendsPointer = $phpcsFile->findNext(\T_EXTENDS, $classPointer);
49+
50+
if ($this->ignoreInheritingClasses && $tokens[$extendsPointer]['code'] === \T_EXTENDS) {
5651
return;
5752
}
5853

5954
$visibilityPointer = $this->findVisibilityPointer($phpcsFile, $variablePointer);
60-
if ($visibilityPointer === null || $tokens[$visibilityPointer]['code'] !== T_PROTECTED) {
55+
56+
if ($visibilityPointer === null || $tokens[$visibilityPointer]['code'] !== \T_PROTECTED) {
6157
return;
6258
}
6359

6460
$fix = $phpcsFile->addFixableError(
6561
'Protected variables and functions inside final class are forbidden',
6662
$variablePointer,
67-
'FinalClassVisibility'
63+
'FinalClassVisibility',
6864
);
6965

70-
if ($fix) {
71-
$phpcsFile->addWarning($visibilityPointer,$variablePointer,
72-
'FinalClassVisibility');
73-
$phpcsFile->fixer->beginChangeset();
74-
$phpcsFile->fixer->replaceToken($visibilityPointer, 'private');
75-
$phpcsFile->fixer->endChangeset();
66+
if (!$fix) {
67+
return;
7668
}
69+
70+
$phpcsFile->fixer->beginChangeset();
71+
$phpcsFile->fixer->replaceToken($visibilityPointer, 'private');
72+
$phpcsFile->fixer->endChangeset();
7773
}
7874

79-
private function findVisibilityPointer(File $phpcsFile, $variablePointer)
75+
/**
76+
* @return mixed
77+
*/
78+
private function findVisibilityPointer(\PHP_CodeSniffer\Files\File $phpcsFile, $variablePointer)
8079
{
81-
$visibilityPointer = $phpcsFile->findPrevious([T_PUBLIC, T_PROTECTED, T_PRIVATE], $variablePointer);
80+
$visibilityPointer = $phpcsFile->findPrevious([\T_PUBLIC, \T_PROTECTED, \T_PRIVATE], $variablePointer);
8281

83-
if(\in_array($phpcsFile->getTokens()[$visibilityPointer]['code'], [T_PUBLIC, T_PROTECTED, T_PRIVATE], true)){
82+
if (\in_array($phpcsFile->getTokens()[$visibilityPointer]['code'], [\T_PUBLIC, \T_PROTECTED, \T_PRIVATE], true)) {
8483
return $visibilityPointer;
8584
}
8685

8786
return null;
8887
}
89-
}
88+
}

InfinityloopCodingStandard/Sniffs/ControlStructures/RequireMultiLineNullCoalesceSniff.php

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,10 @@
44

55
namespace InfinityloopCodingStandard\Sniffs\ControlStructures;
66

7-
use PHP_CodeSniffer\Files\File;
8-
use PHP_CodeSniffer\Sniffs\Sniff;
9-
use SlevomatCodingStandard\Helpers\TokenHelper;
10-
use function array_merge;
11-
use function in_array;
12-
use function strlen;
13-
use function substr;
14-
use const T_COALESCE;
15-
use const T_OPEN_TAG;
16-
use const T_OPEN_TAG_WITH_ECHO;
17-
use const T_WHITESPACE;
18-
19-
class RequireMultiLineNullCoalesceSniff implements Sniff
20-
{
7+
use \SlevomatCodingStandard\Helpers\TokenHelper;
218

9+
class RequireMultiLineNullCoalesceSniff implements \PHP_CodeSniffer\Sniffs\Sniff
10+
{
2211
public const CODE_MULTI_LINE_NULL_COALESCE_OPERATOR_NOT_USED = 'MultiLineNullCoalesceOperatorNotUsed';
2312

2413
private const TAB_INDENT = "\t";
@@ -27,30 +16,30 @@ class RequireMultiLineNullCoalesceSniff implements Sniff
2716
/**
2817
* @return array<int, (int|string)>
2918
*/
30-
public function register(): array
19+
public function register() : array
3120
{
3221
return [
33-
T_COALESCE,
22+
\T_COALESCE,
3423
];
3524
}
3625

37-
/**
38-
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
39-
* @param File $phpcsFile
40-
* @param int $coalescePointer
41-
*/
42-
public function process(File $phpcsFile, $coalescePointer): void
26+
//@phpcs:ignore Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
27+
public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $coalescePointer) : void
4328
{
4429
$tokens = $phpcsFile->getTokens();
4530

46-
/** @var int $variablePointer */
47-
$variablePointer = TokenHelper::findPrevious($phpcsFile, T_VARIABLE, $coalescePointer + 1);
31+
$variablePointer = TokenHelper::findPrevious($phpcsFile, \T_VARIABLE, $coalescePointer + 1);
32+
\assert(\is_int($variablePointer));
4833

4934
if ($tokens[$coalescePointer]['line'] !== $tokens[$variablePointer]['line']) {
5035
return;
5136
}
5237

53-
$fix = $phpcsFile->addFixableError('Null coalesce operator should be reformatted to next line.', $coalescePointer, self::CODE_MULTI_LINE_NULL_COALESCE_OPERATOR_NOT_USED);
38+
$fix = $phpcsFile->addFixableError(
39+
'Null coalesce operator should be reformatted to next line.',
40+
$coalescePointer,
41+
self::CODE_MULTI_LINE_NULL_COALESCE_OPERATOR_NOT_USED,
42+
);
5443

5544
if (!$fix) {
5645
return;
@@ -65,55 +54,67 @@ public function process(File $phpcsFile, $coalescePointer): void
6554
$phpcsFile->fixer->endChangeset();
6655
}
6756

68-
private function getEndOfLineBefore(File $phpcsFile, int $pointer): int
57+
private function getEndOfLineBefore(\PHP_CodeSniffer\Files\File $phpcsFile, int $pointer) : int
6958
{
7059
$tokens = $phpcsFile->getTokens();
7160

7261
$endOfLineBefore = null;
7362

7463
$startPointer = $pointer - 1;
64+
7565
while (true) {
7666
$possibleEndOfLinePointer = TokenHelper::findPrevious(
7767
$phpcsFile,
78-
array_merge([T_WHITESPACE, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO], TokenHelper::$inlineCommentTokenCodes),
79-
$startPointer
68+
\array_merge([\T_WHITESPACE, \T_OPEN_TAG, \T_OPEN_TAG_WITH_ECHO], TokenHelper::$inlineCommentTokenCodes),
69+
$startPointer,
8070
);
81-
if ($tokens[$possibleEndOfLinePointer]['code'] === T_WHITESPACE && $tokens[$possibleEndOfLinePointer]['content'] === $phpcsFile->eolChar) {
71+
72+
if (
73+
$tokens[$possibleEndOfLinePointer]['code'] === \T_WHITESPACE
74+
&& $tokens[$possibleEndOfLinePointer]['content'] === $phpcsFile->eolChar
75+
) {
8276
$endOfLineBefore = $possibleEndOfLinePointer;
77+
8378
break;
8479
}
8580

86-
if ($tokens[$possibleEndOfLinePointer]['code'] === T_OPEN_TAG || $tokens[$possibleEndOfLinePointer]['code'] === T_OPEN_TAG_WITH_ECHO) {
81+
if ($tokens[$possibleEndOfLinePointer]['code'] === \T_OPEN_TAG || $tokens[$possibleEndOfLinePointer]['code'] === \T_OPEN_TAG_WITH_ECHO) {
8782
$endOfLineBefore = $possibleEndOfLinePointer;
83+
8884
break;
8985
}
9086

9187
if (
92-
in_array($tokens[$possibleEndOfLinePointer]['code'], TokenHelper::$inlineCommentTokenCodes, true)
93-
&& substr($tokens[$possibleEndOfLinePointer]['content'], -1) === $phpcsFile->eolChar
88+
\in_array($tokens[$possibleEndOfLinePointer]['code'], TokenHelper::$inlineCommentTokenCodes, true)
89+
&& \substr($tokens[$possibleEndOfLinePointer]['content'], -1) === $phpcsFile->eolChar
9490
) {
9591
$endOfLineBefore = $possibleEndOfLinePointer;
92+
9693
break;
9794
}
9895

9996
$startPointer = $possibleEndOfLinePointer - 1;
10097
}
10198

102-
/** @var int $endOfLineBefore */
10399
$endOfLineBefore = $endOfLineBefore;
100+
\assert(\is_int($endOfLineBefore));
101+
104102
return $endOfLineBefore;
105103
}
106104

107-
private function getIndentation(File $phpcsFile, int $endOfLinePointer): string
105+
private function getIndentation(\PHP_CodeSniffer\Files\File $phpcsFile, int $endOfLinePointer) : string
108106
{
109-
$pointerAfterWhitespace = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $endOfLinePointer + 1);
107+
$pointerAfterWhitespace = TokenHelper::findNextExcluding($phpcsFile, \T_WHITESPACE, $endOfLinePointer + 1);
110108
$actualIndentation = TokenHelper::getContent($phpcsFile, $endOfLinePointer + 1, $pointerAfterWhitespace - 1);
111109

112-
if (strlen($actualIndentation) !== 0) {
113-
return $actualIndentation . (substr($actualIndentation, -1) === self::TAB_INDENT ? self::TAB_INDENT : self::SPACES_INDENT);
110+
if (\strlen($actualIndentation) !== 0) {
111+
return $actualIndentation . (\substr($actualIndentation, -1) === self::TAB_INDENT ? self::TAB_INDENT : self::SPACES_INDENT);
114112
}
115113

116-
$tabPointer = TokenHelper::findPreviousContent($phpcsFile, T_WHITESPACE, self::TAB_INDENT, $endOfLinePointer - 1);
117-
return $tabPointer !== null ? self::TAB_INDENT : self::SPACES_INDENT;
114+
$tabPointer = TokenHelper::findPreviousContent($phpcsFile, \T_WHITESPACE, self::TAB_INDENT, $endOfLinePointer - 1);
115+
116+
return $tabPointer !== null
117+
? self::TAB_INDENT
118+
: self::SPACES_INDENT;
118119
}
119120
}

0 commit comments

Comments
 (0)