Skip to content

Commit 528ceb1

Browse files
authored
Merge pull request #217 from moufmouf/errors_on_bad_namespace
A class declared with a bad namespace causes a crash
2 parents 0526242 + 01ffe2b commit 528ceb1

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
"php": ">=7.2",
1414
"webonyx/graphql-php": "^0.13.4",
1515
"psr/container": "^1",
16-
"doctrine/annotations": "^1.2",
16+
"doctrine/annotations": "^1.7.0",
1717
"doctrine/cache": "^1.8",
18-
"thecodingmachine/class-explorer": "^1.0.2",
18+
"thecodingmachine/class-explorer": "^1.1.0",
1919
"psr/simple-cache": "^1",
2020
"phpdocumentor/reflection-docblock": "^4.3",
2121
"phpdocumentor/type-resolver": "^1.0.1",

src/Mappers/GlobTypeMapper.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,20 @@ protected function getClassList(): array
6464
if ($this->classes === null) {
6565
$this->classes = [];
6666
$explorer = new GlobClassExplorer($this->namespace, $this->cache, $this->globTtl, $this->classNameMapper, $this->recursive);
67-
$classes = $explorer->getClasses();
68-
foreach ($classes as $className) {
69-
if (! class_exists($className) && ! interface_exists($className)) {
70-
continue;
67+
$classes = $explorer->getClassMap();
68+
foreach ($classes as $className => $phpFile) {
69+
if (! class_exists($className, false) && ! interface_exists($className, false)) {
70+
// Let's try to load the file if it was not imported yet.
71+
// We are importing the file manually to avoid triggering the autoloader.
72+
// The autoloader might trigger errors if the file does not respect PSR-4 or if the
73+
// Symfony DebugAutoLoader is installed. (see https://github.com/thecodingmachine/graphqlite/issues/216)
74+
require_once $phpFile;
75+
// Does it exists now?
76+
if (! class_exists($className, false) && ! interface_exists($className, false)) {
77+
continue;
78+
}
7179
}
80+
7281
$refClass = new ReflectionClass($className);
7382
$this->classes[$className] = $refClass;
7483
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
/**
4+
* This class has a bad namespace. It should not trigger errors when using GlobTypeMapper.
5+
*/
6+
class BadNamespaceClass
7+
{
8+
9+
}

0 commit comments

Comments
 (0)