Skip to content

Commit

Permalink
Added check if namespace of file matches folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastian Hofmann committed Jul 8, 2015
1 parent 92878d3 commit a561992
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/rg/tools/phpnsc/ClassScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
namespace rg\tools\phpnsc;

use PhpParser\Node\Stmt\Namespace_;

class ClassScanner {
/**
* @var FilesystemAccess
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a561992

Please sign in to comment.