Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
Skip matching namespace if not found (#11)
Browse files Browse the repository at this point in the history
* skip matching namespace if not found

* update tests
  • Loading branch information
kunicmarko20 authored Apr 7, 2018
1 parent b3de700 commit 8e7b157
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/DependencyInjection/Compiler/AutoRegisterCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public function process(ContainerBuilder $container): void
$this->annotationReader = $container->get('annotation_reader');

foreach ($this->findFiles($container->getParameter('sonata_annotation.directory')) as $file) {
$className = $this->getFullyQualifiedClassName($file);
if (!($className = $this->getFullyQualifiedClassName($file))) {
continue;
}

if (!($annotation = $this->getClassAnnotation($reflection = new \ReflectionClass($className)))) {
continue;
Expand Down Expand Up @@ -58,15 +60,25 @@ private function findFiles(string $directory): \IteratorAggregate
->name('*.php');
}

private function getFullyQualifiedClassName(SplFileInfo $file): string
private function getFullyQualifiedClassName(SplFileInfo $file): ?string
{
return $this->getNamespace($file->getPathname()) . '\\' . $this->getClassName($file->getFilename());
if (!($namespace = $this->getNamespace($file->getPathname()))) {
return null;
}

return $namespace . '\\' . $this->getClassName($file->getFilename());
}

private function getNamespace(string $filePath): string
private function getNamespace(string $filePath): ?string
{
$namespaceLine = preg_grep('/^namespace /', file($filePath));

if (!$namespaceLine) {
return null;
}

preg_match('/namespace (.*);$/', reset($namespaceLine), $match);

return array_pop($match);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Fixtures/MissingNamespaceClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

/**
* @author Marko Kunic <[email protected]>
*/
final class MissingNamespaceClass
{
}

0 comments on commit 8e7b157

Please sign in to comment.