Skip to content
Open
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"psr/container": "^2.0.2",
"psr/simple-cache": "^2.0|^3.0",
"sebastian/diff": "^6.0.2|^7.0.0",
"slevomat/coding-standard": "^8.22.1",
"squizlabs/php_codesniffer": "^3.13.5",
"slevomat/coding-standard": "^8.23",
"squizlabs/php_codesniffer": "^4.0",
"symfony/cache": "^7.4.5|^8.0.8",
"symfony/console": "^7.4.4|^8.0.8",
"symfony/finder": "^7.4.5|^8.0.8",
Expand Down
22 changes: 13 additions & 9 deletions src/Domain/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,26 @@ public function disableFix(): void
* {@inheritdoc}
*/
protected function addMessage(
$isError,
$message,
$line,
$column,
$sniffClassOrCode,
$data,
$severity,
$isFixable = false
bool $isError,
string $message,
int $line,
int $column,
string $sniffClassOrCode,
array $data,
int $severity,
bool $isFixable = false
): bool {
$message = $data !== [] ? vsprintf($message, $data) : $message;

if ($isFixable && $this->isFixable) {
if ($this->fixEnabled) {
$this->activeSniff->addFileFixed($this->fileInfo->getRelativePathname());
} else {
$this->fixableCount++;
if ($isError) {
$this->fixableErrorCount++;
} else {
$this->fixableWarningCount++;
}
}

return true;
Expand Down
7 changes: 1 addition & 6 deletions src/Domain/Insights/SniffDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ public function register(): array
return $this->sniff->register();
}

/**
* @param int $stackPtr
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*/
public function process(File $file, $stackPtr): int|null
public function process(File $file, int $stackPtr): int|null
{
if ($file instanceof InsightFile && $this->skipFilesFromIgnoreFiles($file)) {
return null;
Expand Down
4 changes: 1 addition & 3 deletions src/Domain/Metrics/Code/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
use PHP_CodeSniffer\Standards\Generic\Sniffs\Strings\UnnecessaryStringConcatSniff;
use PHP_CodeSniffer\Standards\PSR12\Sniffs\Keywords\ShortFormTypeKeywordsSniff;
use PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\SwitchDeclarationSniff;
use PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\LanguageConstructSpacingSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\PHP\EvalSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\LanguageConstructSpacingSniff;
use PHP_CodeSniffer\Standards\Zend\Sniffs\Debug\CodeAnalyzerSniff;
use PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer;
use PhpCsFixer\Fixer\ArrayNotation\NoMultilineWhitespaceAroundDoubleArrowFixer;
use PhpCsFixer\Fixer\ArrayNotation\NormalizeIndexBraceFixer;
Expand Down Expand Up @@ -80,7 +79,6 @@ public function getInsights(): array
{
return [
UnusedVariableSniff::class,
CodeAnalyzerSniff::class,
SwitchDeclarationSniff::class,
LanguageConstructSpacingSniff::class,
UselessVariableSniff::class,
Expand Down
2 changes: 0 additions & 2 deletions src/Domain/Metrics/Code/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use NunoMaduro\PhpInsights\Domain\Contracts\HasPercentage;
use NunoMaduro\PhpInsights\Domain\Contracts\HasValue;
use NunoMaduro\PhpInsights\Domain\Insights\ForbiddenDefineFunctions;
use PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\CallTimePassByReferenceSniff;
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\DeprecatedFunctionsSniff;
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
use PHP_CodeSniffer\Standards\PSR12\Sniffs\Functions\NullableTypeDeclarationSniff;
Expand Down Expand Up @@ -46,7 +45,6 @@ public function getInsights(): array
return [
UnusedInheritedVariablePassedToClosureSniff::class,
UnusedParameterSniff::class,
CallTimePassByReferenceSniff::class,
DeprecatedFunctionsSniff::class,
NullableTypeDeclarationSniff::class,
StaticClosureSniff::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Metrics/Style/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
use PHP_CodeSniffer\Standards\PSR2\Sniffs\Files\ClosingTagSniff;
use PHP_CodeSniffer\Standards\PSR2\Sniffs\Files\EndFileNewlineSniff;
use PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods\FunctionClosingBraceSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\LanguageConstructSpacingSniff;
use PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\LanguageConstructSpacingSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff;
use PhpCsFixer\Fixer\ArrayNotation\NoTrailingCommaInSinglelineArrayFixer;
use PhpCsFixer\Fixer\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer;
Expand Down
13 changes: 3 additions & 10 deletions src/Domain/Sniffs/ForbiddenSetterSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,10 @@ public function register(): array
return [T_FUNCTION];
}

/**
* Runs the sniff on a file.
*
* @param int $position
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*/
public function process(File $file, $position): void
public function process(File $file, int $position): void
{
$methodName = $file->getDeclarationName($position);
if ($methodName === null) {
if ($methodName === '' || $methodName === null) {
return;
}

Expand All @@ -52,7 +45,7 @@ public function process(File $file, $position): void
return;
}

$file->addError(self::ERROR_MESSAGE, $position, self::class);
$file->addError(self::ERROR_MESSAGE, $position, 'PhpInsights.Sniffs.ForbiddenSetter');
}

/**
Expand Down
6 changes: 1 addition & 5 deletions tests/Feature/Fix/Fixtures/ParamTypeHint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ final class ParamTypeHint
{
/**
* Do some calculation
* @param int $a
* @param int $b
*
* @return int
*/
public function sum($a, $b)
public function sum(int $a, int $b): int
{
return $a + $b;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Fix/Fixtures/UnorderedUse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

declare(strict_types=1);

use NunoMaduro\PhpInsights\Domain\Analyser;
use NunoMaduro\PhpInsights\Application\ConfigResolver;
use NunoMaduro\PhpInsights\Domain\Analyser;

/**
* This test class is for testing if fix from CSFixer is correctly applied
Expand Down
37 changes: 22 additions & 15 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,32 +130,39 @@ final public static function prepareFixtureWithSniff(
string $fixtureFile,
array $properties = []
): LocalFile {
$sniffs = [self::getFilePathFromClass($sniffClassName)];

$config = new Config();
$config->standards = [];

$ruleName = str_replace(
'Sniff',
'',
class_basename($sniffClassName)
);

/** @var Ruleset $ruleset */
$ruleset = (new ReflectionClass(Ruleset::class))
->newInstanceWithoutConstructor();

$msgCacheProperty = (new ReflectionClass(Ruleset::class))->getProperty('msgCache');
$msgCacheProperty->setValue($ruleset, new MessageCollector());

$ruleset->ruleset = [
"PhpInsights.Sniffs.{$ruleName}" => [
'properties' => $properties,
],
];
// Manually register the sniff to bypass PHPCS naming convention validation
// which rejects sniffs outside the Standard\Sniffs\Category\SniffName namespace
$sniffObject = new $sniffClassName();

foreach ($properties as $name => $value) {
$sniffObject->{$name} = $value;
}

$sniffCode = 'PhpInsights.Sniffs.' . str_replace('Sniff', '', class_basename($sniffClassName));

$ruleset->registerSniffs($sniffs, [], []);
$ruleset->populateTokenListeners();
$ruleset->sniffs[$sniffClassName] = $sniffObject;
$ruleset->sniffCodes[$sniffCode] = $sniffClassName;
$ruleset->tokenListeners = [];

foreach ($sniffObject->register() as $token) {
$ruleset->tokenListeners[$token][$sniffClassName] = [
'class' => $sniffClassName,
'source' => $sniffCode,
'tokenizers' => ['PHP'],
'ignore' => [],
'include' => [],
];
}

return new LocalFile($fixtureFile, $ruleset, $config);
}
Expand Down