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
5 changes: 5 additions & 0 deletions config/phpstan.services.neon
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ services:
class: \FiveLab\Component\CiRules\PhpStan\ForbiddenPassArgumentAsReferenceRule
arguments: [ PhpParser\Node\Stmt\ClassMethod ]
tags: [ phpstan.rules.rule ]

-
class: \FiveLab\Component\CiRules\PhpStan\MethodCallConsistencyRule
arguments: [ @reflectionProvider ]
tags: [ phpstan.rules.rule ]
1 change: 1 addition & 0 deletions src/PhpCs/FiveLab/ErrorCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ final class ErrorCodes
public const LINE_AFTER_NOT_ALLOWED = 'LineAfterNotAllowed';
public const LINE_BEFORE_NOT_ALLOWED = 'LineBeforeNotAllowed';
public const MISSED_CONSTANT_TYPE = 'MissedConstantType';
public const NAMESPACE_WRONG = 'NamespaceWrong';
}
120 changes: 120 additions & 0 deletions src/PhpCs/FiveLab/Sniffs/Namespace/NamespaceSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

/*
* 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
*/

declare(strict_types = 1);

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

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

class NamespaceSniff implements Sniff
{
public function register(): array
{
return [T_NAMESPACE];
}

public function process(File $phpcsFile, mixed $stackPtr): void
{
$tokens = $phpcsFile->getTokens();
$endToken = $phpcsFile->findNext(T_SEMICOLON, $stackPtr);

if (false === $endToken) {
return;
}

$declaredNamespace = '';

for ($i = $stackPtr + 1; $i < $endToken; $i++) {
$declaredNamespace .= $tokens[$i]['content'];
}

$expectedNamespace = $this->getExpectedNamespace($phpcsFile);

if (\trim($declaredNamespace) !== $expectedNamespace) {
$phpcsFile->addError(
'Expected namespace "%s", found "%s".',
$stackPtr,
ErrorCodes::NAMESPACE_WRONG,
[$expectedNamespace, $declaredNamespace]
);
}
}

private function getExpectedNamespace(File $phpcsFile): ?string
{
$composerJsonPath = $this->findProjectComposerJson($phpcsFile);
$composerJson = $this->loadComposerJson($composerJsonPath);

$psr4 = \array_merge(
$composerJson['autoload']['psr-4'] ?? [],
$composerJson['autoload-dev']['psr-4'] ?? []
);

foreach ($psr4 as $namespace => $directory) {
$normalizedDirectory = \realpath(\dirname($composerJsonPath).'/'.$directory);
$normalizedFilePath = \realpath($phpcsFile->path);

if ($normalizedDirectory && $normalizedFilePath && \str_starts_with($normalizedFilePath, $normalizedDirectory)) {
$relativePath = \substr($normalizedFilePath, \strlen($normalizedDirectory) + 1);
$expectedNamespace = \rtrim($namespace, '\\').'\\'.\str_replace('/', '\\', \dirname($relativePath));

return \trim($expectedNamespace, '\\, .');
}
}

return null;
}

/**
* Load composer.json
*
* @param string $composerJsonPath
*
* @return array<mixed>
* @throws \JsonException
*/
private function loadComposerJson(string $composerJsonPath): array
{
if (!\file_exists($composerJsonPath)) {
throw new \RuntimeException(\sprintf('composer.json not found at "%s".', $composerJsonPath));
}

$content = \file_get_contents($composerJsonPath);

$decoded = $content ? \json_decode($content, true, flags: JSON_THROW_ON_ERROR) : null;

if (!\is_array($decoded)) {
throw new \RuntimeException(\sprintf('Invalid composer.json format at "%s".', $composerJsonPath));
}

return $decoded;
}

private function findProjectComposerJson(File $phpcsFile): string
{
$currentDir = \dirname($phpcsFile->getFilename());

while ($currentDir !== \dirname($currentDir)) {
$composerJsonPath = $currentDir.'/composer.json';

if (\file_exists($composerJsonPath)) {
return $composerJsonPath;
}

$currentDir = \dirname($currentDir);
}

throw new \RuntimeException('composer.json not found in the project root.');
}
}
60 changes: 60 additions & 0 deletions src/PhpCs/FiveLab/Sniffs/Strings/AsciiSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* 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
*/

declare(strict_types = 1);

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

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

class AsciiSniff implements Sniff
{
public function register(): array
{
return [
T_VARIABLE,
T_CLASS,
T_INTERFACE,
T_FUNCTION,
T_STRING,
T_CONSTANT_ENCAPSED_STRING,
T_DOUBLE_QUOTED_STRING,
T_COMMENT,
T_DOC_COMMENT_STRING,
];
}

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

foreach (\mb_str_split($content) as $char) {
$ascii = \ord($char);

if (10 !== $ascii && (32 > $ascii || 127 < $ascii)) {
$forbiddenSymbols[] = $ascii;
}
}

if ($forbiddenSymbols) {
$phpcsFile->addError(
'Use not ASCII printable symbols is forbidden: "%s"',
$stackPtr,
ErrorCodes::PROHIBITED,
[\implode(', ', $forbiddenSymbols)]
);
}
}
}
Loading