From 75a43ae08f7c702909bb2cfda2797b7f21875d26 Mon Sep 17 00:00:00 2001 From: Pavel Alexeev Date: Thu, 6 Jan 2022 01:03:30 +0300 Subject: [PATCH 1/2] #38 Try search parent class in the same namespace --- src/Parser/ClassParser.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Parser/ClassParser.php b/src/Parser/ClassParser.php index e23a069..9cec5c6 100644 --- a/src/Parser/ClassParser.php +++ b/src/Parser/ClassParser.php @@ -190,11 +190,21 @@ private function getParentClassStatements(): ?array return []; } - if (null !== $usedClasses[$this->getParentClassName()]) { + if (null !== @$usedClasses[$this->getParentClassName()]) { $parentClass = $usedClasses[$this->getParentClassName()]; } - $rc = new ReflectionClass($parentClass); + try { + $rc = new ReflectionClass($parentClass); + } + catch (\ReflectionException $e) { + try { + $rc = new ReflectionClass($this->getNamespace() . '\\' . $parentClass); + } + catch (\ReflectionException $e) { + throw new ReflectionException("Parent class [{$parentClass}] for [{$this->getNamespace()}\\{$this->getClassName()}] not found!", $e->getCode(), $e); + } + } $filename = $rc->getFileName(); if (false === $filename) { From 2b0a85e0d418a2f334329b454e76564340f6aa3a Mon Sep 17 00:00:00 2001 From: Pavel Alexeev Date: Thu, 6 Jan 2022 02:07:11 +0300 Subject: [PATCH 2/2] #38, #40 Use reflection for robust resolving parent class --- src/Parser/ClassParser.php | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/Parser/ClassParser.php b/src/Parser/ClassParser.php index 9cec5c6..b4aa751 100644 --- a/src/Parser/ClassParser.php +++ b/src/Parser/ClassParser.php @@ -184,28 +184,18 @@ private function getParentClassStatements(): ?array { /** @var class-string[] $usedClasses */ $usedClasses = $this->getUsedClasses(); - $parentClass = $this->getParentClassName(); - - if (null === $parentClass) { - return []; - } - - if (null !== @$usedClasses[$this->getParentClassName()]) { - $parentClass = $usedClasses[$this->getParentClassName()]; - } try { - $rc = new ReflectionClass($parentClass); + $pc = (new ReflectionClass($this->getNamespace() . '\\' . $this->getClassName()))->getParentClass(); } catch (\ReflectionException $e) { - try { - $rc = new ReflectionClass($this->getNamespace() . '\\' . $parentClass); - } - catch (\ReflectionException $e) { - throw new ReflectionException("Parent class [{$parentClass}] for [{$this->getNamespace()}\\{$this->getClassName()}] not found!", $e->getCode(), $e); - } + throw new ReflectionException("Can't get parent class for [{$this->getNamespace()}\\{$this->getClassName()}]!", $e->getCode(), $e); } - $filename = $rc->getFileName(); + if (false === $pc) { + return []; + } + + $filename = $pc->getFileName(); if (false === $filename) { return [];