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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ start:

fix:
# phpstan -l (level 0-9, max - newest)
docker-compose -f .docker/docker-compose.yml exec php vendor/bin/phpstan analyse app -l max
docker-compose -f .docker/docker-compose.yml exec php vendor/bin/phpstan analyse -c phpstan/config.neon --memory-limit 2048M
77 changes: 77 additions & 0 deletions phpstan/Rules/Psr4NamespaceCheckRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php declare(strict_types=1);

namespace Pd\PHPStan\Rules;

class Psr4NamespaceCheckRule implements \PHPStan\Rules\Rule
{

public function getNodeType(): string
{
return \PhpParser\Node\Stmt\ClassLike::class;
}


/**
* @return array<string>
*
* @param \PhpParser\Node\Stmt\ClassLike $node
*/
public function processNode(
\PhpParser\Node $node,
\PHPStan\Analyser\Scope $scope,
): array
{
$namespace = (string) $node->namespacedName;
if ($namespace === '') {
return [
'Class does not have a defined namespace',
];
}

$filePath = \str_replace('\\', '/', $scope->getFile());

if (\preg_match('@/(app(-[a-z-]+)?)/@', $filePath, $matches) === FALSE) {
return [];
}

if ($matches === []) {
return [];
}

$directory = $matches[1];
$ns = $this->getNamespaceFromDirectoryName($directory);

$positionInDirectoryNamed = \strpos($filePath, $directory . '/');
if ($positionInDirectoryNamed === FALSE) {
return [];
}

$namespacedName = \preg_replace('/' . $directory . '/', $ns, \substr($filePath, $positionInDirectoryNamed), 1);

$filename = \pathinfo($namespacedName, \PATHINFO_FILENAME);
$pathname = \pathinfo($namespacedName, \PATHINFO_DIRNAME);

$pathname = "Pd" . \substr($pathname, 3);

$psr4Namespace = \str_replace('/', '\\', \sprintf('%s/%s', $pathname, $filename));

if ($psr4Namespace === $namespace) {
return [];
}

return [
\sprintf('Class like namespace "%s" does not follow PSR-4 configuration, use %s', $namespace, $psr4Namespace),
];
}


private function getNamespaceFromDirectoryName(string $directory): string
{
return \implode(
'', \array_map(static function (string $item): string {
return \ucfirst($item);
}, \explode('-', $directory))
);
}

}
8 changes: 8 additions & 0 deletions phpstan/config.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: max

paths:
- app

rules:
- app\PHPStan\Rules\Psr4NamespaceCheckRule