Fix constant namespace extraction

This commit is contained in:
Maxim.Kolmakov 2019-06-26 12:41:03 +02:00
parent 456b11149c
commit 639c159cfb
3 changed files with 22 additions and 13 deletions

View File

@ -3,9 +3,6 @@ declare(strict_types=1);
namespace StubTests\Model; namespace StubTests\Model;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\NodeAbstract;
abstract class BasePHPElement abstract class BasePHPElement
{ {
public $name; public $name;
@ -28,16 +25,6 @@ abstract class BasePHPElement
abstract public function readMutedProblems($jsonData); abstract public function readMutedProblems($jsonData);
protected function getConstantFQN(NodeAbstract $node, string $nodeName): string
{
$namespace = '';
if ($node->getAttribute('parent') instanceof Namespace_ && !empty($node->getAttribute('parent')->name)) {
$namespace = '\\' . implode('\\', $node->getAttribute('parent')->name->parts) . '\\';
}
return $namespace . $nodeName;
}
protected function getFQN($node): string protected function getFQN($node): string
{ {
$fqn = ''; $fqn = '';

View File

@ -5,6 +5,8 @@ namespace StubTests\Model;
use PhpParser\Node\Const_; use PhpParser\Node\Const_;
use PhpParser\Node\Stmt\ClassConst; use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\NodeAbstract;
use ReflectionClassConstant; use ReflectionClassConstant;
use stdClass; use stdClass;
@ -55,6 +57,17 @@ class PHPConst extends BasePHPElement
return null; return null;
} }
protected function getConstantFQN(NodeAbstract $node, string $nodeName): string
{
$namespace = '';
$parentParentNode = $node->getAttribute('parent')->getAttribute('parent');
if ($parentParentNode instanceof Namespace_ && !empty($parentParentNode->name)) {
$namespace = '\\' . implode('\\', $parentParentNode->name->parts) . '\\';
}
return $namespace . $nodeName;
}
public function readMutedProblems($jsonData): void public function readMutedProblems($jsonData): void
{ {
/**@var stdClass $constant */ /**@var stdClass $constant */

View File

@ -37,6 +37,9 @@ class StubsContainer
*/ */
public function addConstant(PHPConst $constant): void public function addConstant(PHPConst $constant): void
{ {
if (array_key_exists($constant->name, $this->constants)) {
throw new \Exception($constant->name . " is already defined in stubs");
}
$this->constants[$constant->name] = $constant; $this->constants[$constant->name] = $constant;
} }
@ -82,6 +85,9 @@ class StubsContainer
*/ */
public function addClass(PHPClass $class): void public function addClass(PHPClass $class): void
{ {
if (array_key_exists($class->name, $this->classes)) {
throw new \Exception($class->name . " is already defined in stubs");
}
$this->classes[$class->name] = $class; $this->classes[$class->name] = $class;
} }
@ -111,6 +117,9 @@ class StubsContainer
*/ */
public function addInterface(PHPInterface $interface): void public function addInterface(PHPInterface $interface): void
{ {
if (array_key_exists($interface->name, $this->interfaces)) {
throw new \Exception($interface->name . " is already defined in stubs");
}
$this->interfaces[$interface->name] = $interface; $this->interfaces[$interface->name] = $interface;
} }
} }