Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions features/semantic-class-errors.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Feature: Semantic errors in classes
As a user
I want all semantic invalid classes to be excluded from index
So that I can have project with broken classes or deps

Scenario: Class extending itself
Given there is a file with:
"""
<?php

class C extends C {}
"""

Scenario: Parent extends Child
Given there is a file with:
"""
<?php

class E extends F {}
class F extends E {}
"""

Scenario: Parent extends Sub Child
Given there is a file with:
"""
<?php

class G extends H {}
class H extends I {}
class I extends G {}
"""

7 changes: 4 additions & 3 deletions src/Padawan/Domain/Project/Node/ClassData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Padawan\Domain\Project\Node;

use Padawan\Domain\Project\FQCN;
use Padawan\Domain\Project\FQN;
use Padawan\Domain\Project\FQCN;
use Padawan\Parser\Exception\SemanticError;
use Padawan\Domain\Project\Collection\ConstCollection;
use Padawan\Domain\Project\Collection\MethodsCollection;
use Padawan\Domain\Project\Collection\PropertiesCollection;
use Padawan\Domain\Project\Collection\ConstCollection;

/**
* @property $properties
Expand Down Expand Up @@ -64,7 +65,7 @@ public function getName()
public function setParent($parent)
{
if ($this === $parent) {
throw new \Exception("Parent class and child class could not be same");
throw new SemanticError("Parent class and child class could not be same");
}
$this->parent = null;
if ($parent instanceof ClassData) {
Expand Down
14 changes: 14 additions & 0 deletions src/Padawan/Framework/Domain/Project/InMemoryIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ public function getFunctions()
}

public function addClass(ClassData $class) {
if (!$this->isSemanticsCorrect($class)) {
return;
}
$this->classes[$class->fqcn->toString()] = $class;
if ($class->getParent() instanceof FQCN) {
$this->addExtend($class, $class->getParent());
Expand Down Expand Up @@ -238,4 +241,15 @@ private function hasCoreIndex()
{
return $this !== self::$coreIndex && !empty(self::$coreIndex);
}

private function isSemanticsCorrect(ClassData $class)
{
if ($class->getParent() === $class) {
return false;
}
if ($class->getParent() instanceof FQCN
&& $class->getParent()->toString() === $class->fqcn->toString()) {
return false;
}
}
}
13 changes: 13 additions & 0 deletions src/Padawan/Parser/Exception/SemanticError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Padawan\Parser\Exception;

use Exception;

/**
* Class SemanticError
* @author mkusher
*/
class SemanticError extends Exception
{
}