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
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ end_of_line = crlf

[*.yml]
indent_size = 2

[*.neon]
indent_style = tab
4 changes: 3 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

# Ignore files for distribution archives, generated using `git archive`
.editorconfig export-ignore
.git export-ignore
.github export-ignore
.gitattributes export-ignore
.gitignore export-ignore
phpcs.xml export-ignore
phpunit.xml.dist export-ignore
/CakePHP/Tests export-ignore
phpstan.neon export-ignore
.phive export-ignore
16 changes: 11 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ on:

jobs:
testsuite:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
php-version: ['8.1', '8.2', '8.3']
php-version: ['8.1', '8.2', '8.3', '8.4']
dependencies: ['highest']
include:
- php-version: '8.1'
Expand All @@ -40,20 +40,26 @@ jobs:

cs-stan:
name: Coding Standard & Static Analysis
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
tools: cs2pr
tools: phive, cs2pr
coverage: none

- name: Composer install
uses: ramsey/composer-install@v3

- name: Install PHP tools with phive.
run: "phive install --trust-gpg-keys 'CF1A108D0E7AE720,51C67305FFC2E5C0,12CE0F1D262429A5'"

- name: Run PHP CodeSniffer
run: vendor/bin/phpcs --report=checkstyle | cs2pr

- name: Run PHPStan
run: tools/phpstan analyse --error-format=github
4 changes: 4 additions & 0 deletions .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpstan" version="2.0.3" installed="2.0.3" location="./tools/phpstan" copy="false"/>
</phive>
13 changes: 9 additions & 4 deletions CakePHP/Sniffs/Classes/ReturnTypeHintSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->addError(
'Chaining methods (@return $this) should not have any return-type-hint.',
$startIndex,
'InvalidSelf'
'InvalidSelf',
);

return;
Expand All @@ -76,7 +76,7 @@ public function process(File $phpcsFile, $stackPtr)
$fix = $phpcsFile->addFixableError(
'Chaining methods (@return $this) should not have any return-type-hint (Remove "self").',
$startIndex,
'InvalidSelf'
'InvalidSelf',
);
if (!$fix) {
return;
Expand Down Expand Up @@ -175,7 +175,7 @@ protected function assertNotThisOrStatic(File $phpCsFile, int $stackPointer): vo
$phpCsFile->addError(
'Class name repeated, expected `self` or `$this`.',
$classNameIndex,
'InvalidClass'
'InvalidClass',
);
}
}
Expand Down Expand Up @@ -228,9 +228,14 @@ protected function getClassNameWithNamespace(File $phpCsFile): ?string
return null;
}

$classPointer = $phpCsFile->findPrevious(TokenHelper::$typeKeywordTokenCodes, $lastToken);
if (!$classPointer) {
return null;
}

return ClassHelper::getFullyQualifiedName(
$phpCsFile,
$phpCsFile->findPrevious(TokenHelper::$typeKeywordTokenCodes, $lastToken)
$classPointer,
);
}
}
2 changes: 1 addition & 1 deletion CakePHP/Sniffs/Commenting/DocBlockAlignmentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function process(File $phpcsFile, $stackPtr)
$commentBorder = $phpcsFile->findNext(
[T_DOC_COMMENT_STAR, T_DOC_COMMENT_CLOSE_TAG],
$searchToken,
$commentClose + 1
$commentClose + 1,
);
if ($commentBorder !== false) {
$tokensToIndent[$commentBorder] = $codeIndentation + 1;
Expand Down
12 changes: 6 additions & 6 deletions CakePHP/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public function process(File $phpcsFile, $stackPtr)
$docCommentEnd = $phpcsFile->findPrevious(
[T_DOC_COMMENT_CLOSE_TAG, T_SEMICOLON, T_CLOSE_CURLY_BRACKET, T_OPEN_CURLY_BRACKET],
$stackPtr - 1,
null
null,
);
if ($docCommentEnd === false || $tokens[$docCommentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
$phpcsFile->addError(
'Missing doc comment for function %s()',
$stackPtr,
'Missing',
[$phpcsFile->getDeclarationName($stackPtr)]
[$phpcsFile->getDeclarationName($stackPtr)],
);

return;
Expand All @@ -77,14 +77,14 @@ public function process(File $phpcsFile, $stackPtr)
$attribute = $phpcsFile->findNext(
[T_ATTRIBUTE],
$lastEndToken + 1,
$stackPtr
$stackPtr,
);
if ($attribute !== false) {
if ($tokens[$lastEndToken]['line'] !== $tokens[$attribute]['line'] - 1) {
$phpcsFile->addError(
'There must be no blank lines after the function comment or attribute',
$lastEndToken,
'SpacingAfter'
'SpacingAfter',
);

return;
Expand All @@ -98,7 +98,7 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->addError(
'There must be no blank lines after the function comment or attribute',
$lastEndToken,
'SpacingAfter'
'SpacingAfter',
);
}

Expand Down Expand Up @@ -152,7 +152,7 @@ protected function processThrows(File $phpcsFile, int $stackPtr, int $commentSta
if ($tokens[$tag + 2]['code'] === T_DOC_COMMENT_STRING) {
$matches = [];
preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[$tag + 2]['content'], $matches);
$exception = $matches[1];
$exception = $matches[1] ?? null;
}

if ($exception === null) {
Expand Down
22 changes: 13 additions & 9 deletions CakePHP/Sniffs/Commenting/TypeHintSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use PHPStan\PhpDocParser\ParserConfig;

/**
* Verifies order of types in type hints
Expand Down Expand Up @@ -128,7 +129,7 @@ public function process(File $phpcsFile, $stackPtr)
'%s type hint is not formatted properly, expected "%s"',
$tag,
'IncorrectFormat',
[$tokens[$tag]['content'], $sortedTypeHint]
[$tokens[$tag]['content'], $sortedTypeHint],
);
if (!$fix) {
continue;
Expand All @@ -140,7 +141,7 @@ public function process(File $phpcsFile, $stackPtr)
'%s %s %s',
$sortedTypeHint,
$valueNode->variableName,
$valueNode->description
$valueNode->description,
));
if ($tagComment[-1] === ' ') {
// tags above variables in code have a trailing space
Expand All @@ -152,13 +153,13 @@ public function process(File $phpcsFile, $stackPtr)
$sortedTypeHint,
$valueNode->isVariadic ? '...' : '',
$valueNode->parameterName,
$valueNode->description
$valueNode->description,
));
} elseif ($valueNode instanceof ReturnTagValueNode) {
$newComment = trim(sprintf(
'%s %s',
$sortedTypeHint,
$valueNode->description
$valueNode->description,
));
}

Expand Down Expand Up @@ -278,10 +279,10 @@ protected function getSortedTypeHint(array $types): string
protected function renderUnionTypes(array $typeNodes): string
{
// Remove parenthesis added by phpstan around union and intersection types
return preg_replace(
return (string)preg_replace(
['/ ([\|&]) /', '/<\(/', '/\)>/', '/\), /', '/, \(/'],
['${1}', '<', '>', ', ', ', '],
implode('|', $typeNodes)
implode('|', $typeNodes),
);
}

Expand All @@ -294,13 +295,16 @@ protected static function getValueNode(string $tagName, string $tagComment): Php
{
static $phpDocParser;
if (!$phpDocParser) {
$constExprParser = new ConstExprParser();
$phpDocParser = new PhpDocParser(new TypeParser($constExprParser), $constExprParser);
$config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true]);

$constExprParser = new ConstExprParser($config);
$phpDocParser = new PhpDocParser($config, new TypeParser($config, $constExprParser), $constExprParser);
}

static $phpDocLexer;
if (!$phpDocLexer) {
$phpDocLexer = new Lexer();
$config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true]);
$phpDocLexer = new Lexer($config);
}

return $phpDocParser->parseTagValue(new TokenIterator($phpDocLexer->tokenize($tagComment)), $tagName);
Expand Down
2 changes: 1 addition & 1 deletion CakePHP/Sniffs/Formatting/BlankLineBeforeReturnSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function process(File $phpcsFile, $stackPtr)
$fix = $phpcsFile->addFixableError(
'Missing blank line before return statement',
$stackPtr,
'BlankLineBeforeReturn'
'BlankLineBeforeReturn',
);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
Expand Down
52 changes: 0 additions & 52 deletions CakePHP/Sniffs/Functions/ClosureDeclarationSniff.php

This file was deleted.

4 changes: 4 additions & 0 deletions CakePHP/Sniffs/NamingConventions/ValidFunctionNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public function __construct()
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
{
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
return;
}

$className = $phpcsFile->getDeclarationName($currScope);
$errorData = [$className . '::' . $methodName];

Expand Down
2 changes: 1 addition & 1 deletion CakePHP/Sniffs/PHP/SingleQuoteSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function process(File $phpcsFile, $stackPtr)
$fix = $phpcsFile->addFixableError(
'Use single instead of double quotes for simple strings.',
$stackPtr,
'UseSingleQuote'
'UseSingleQuote',
);
if ($fix) {
$content = substr($content, 1, -1);
Expand Down
15 changes: 12 additions & 3 deletions CakePHP/Sniffs/WhiteSpace/FunctionSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public function process(File $phpCsFile, $stackPointer)
$closingParenthesisIndex = $tokens[$openingParenthesisIndex]['parenthesis_closer'];

$semicolonIndex = $phpCsFile->findNext(T_SEMICOLON, $closingParenthesisIndex + 1);
if (!$semicolonIndex) {
return;
}

$nextContentIndex = $phpCsFile->findNext(T_WHITESPACE, $semicolonIndex + 1, null, true);

Expand All @@ -65,7 +68,7 @@ public function process(File $phpCsFile, $stackPointer)
$fix = $phpCsFile->addFixableError(
'Every function/method needs a newline afterwards',
$closingParenthesisIndex,
'Abstract'
'Abstract',
);
if ($fix) {
$phpCsFile->fixer->addNewline($semicolonIndex);
Expand All @@ -84,6 +87,9 @@ public function process(File $phpCsFile, $stackPointer)
}

$nextContentIndex = $phpCsFile->findNext(T_WHITESPACE, $closingBraceIndex + 1, null, true);
if (!$nextContentIndex) {
return;
}

// Do not mess with the end of the class
if ($tokens[$nextContentIndex]['code'] === T_CLOSE_CURLY_BRACKET) {
Expand All @@ -108,7 +114,7 @@ protected function assertNewLineAtTheEnd(File $phpCsFile, int $closingBraceIndex
$fix = $phpCsFile->addFixableError(
'Every function/method needs a newline afterwards',
$closingBraceIndex,
'Concrete'
'Concrete',
);
if ($fix) {
$phpCsFile->fixer->addNewline($closingBraceIndex);
Expand Down Expand Up @@ -147,6 +153,9 @@ protected function assertNewLineAtTheBeginning(File $phpCsFile, int $stackPointe
}

$prevContentIndex = $phpCsFile->findPrevious(T_WHITESPACE, $firstTokenInLineIndex - 1, null, true);
if (!$prevContentIndex) {
return;
}

// Do not mess with the start of the class
if ($tokens[$prevContentIndex]['code'] === T_OPEN_CURLY_BRACKET) {
Expand All @@ -160,7 +169,7 @@ protected function assertNewLineAtTheBeginning(File $phpCsFile, int $stackPointe
$fix = $phpCsFile->addFixableError(
'Every function/method needs a newline before',
$firstTokenInLineIndex,
'Concrete'
'Concrete',
);
if ($fix) {
$phpCsFile->fixer->addNewline($prevContentIndex);
Expand Down
Loading