From a5619926451dd28ea91f19b7f1b05cdc60564758 Mon Sep 17 00:00:00 2001 From: Bastian Hofmann Date: Wed, 8 Jul 2015 11:48:04 +0200 Subject: [PATCH] Added check if namespace of file matches folder structure --- src/rg/tools/phpnsc/ClassScanner.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/rg/tools/phpnsc/ClassScanner.php b/src/rg/tools/phpnsc/ClassScanner.php index a256e7f..5d41d16 100644 --- a/src/rg/tools/phpnsc/ClassScanner.php +++ b/src/rg/tools/phpnsc/ClassScanner.php @@ -9,6 +9,8 @@ */ namespace rg\tools\phpnsc; +use PhpParser\Node\Stmt\Namespace_; + class ClassScanner { /** * @var FilesystemAccess @@ -46,10 +48,29 @@ public function __construct(FilesystemAccess $filesystem, $root, $namespaceVendo * @param array $files */ public function parseFilesForClassesAndInterfaces($files) { + $parser = new \PhpParser\Parser(new \PhpParser\Lexer); + $progressbar = new Progressbar($this->output, count($files)); foreach ($files as $file) { $namespace = (string)new NamespaceString($this->namespaceVendor, $this->root, $file); $originalFileContent = $this->filesystem->getFile($file); + + try { + $stmts = $parser->parse($originalFileContent); + $firstStatement = $stmts[0]; + if ($firstStatement instanceof Namespace_) { + $namespaceOfFile = implode('\\', $firstStatement->name->parts); + if ($namespace !== $namespaceOfFile) { + $this->output->addError('Namespace does not match folder structure, got ' . $namespaceOfFile . ' expected ' . $namespace, $file, $firstStatement->getLine()); + } + } + } catch (\PhpParser\Error $e) { + $this->output->addError( + 'Parse Error: ' . $e->getMessage(), + $file, + 1 + ); + } $fileContent = $this->cleanContent($originalFileContent); $this->parseDefinedEntities($file, $namespace, $fileContent, $originalFileContent); $this->parseUsedEntities($file, $namespace, $fileContent, $originalFileContent);