Skip to content

Commit

Permalink
Merge pull request #6 from CircleOfNice/reflection-getTraits
Browse files Browse the repository at this point in the history
added reflection functionality
  • Loading branch information
TobiasHauck committed Jun 13, 2015
2 parents 8340a74 + d4fff68 commit 5c231cc
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 13 deletions.
18 changes: 12 additions & 6 deletions Container/ImmutableContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ final class ImmutableContainer implements Container {
use Exceptions;

/**
* @var Doctrine\Common\Collections\ArrayCollection stores the values of this container
* @var ArrayCollection stores the values of this container
*/
private $collection;

/**
* constructor takes an array to set all values
*
* @param array $array
* @return $this
* @param array $array
* @return ImmutableContainer
*/
public function __construct(array $array) {
$this->collection = new ArrayCollection($array);
Expand All @@ -38,13 +38,19 @@ public function __construct(array $array) {
/**
* (non-PHPdoc)
* @see \Circle\UtilityBundle\Interfaces\Container::get()
*
* @param string $key
*/
public function get($key) {
return $this->has($key) ? $this->collection->get($key) : $this->_noKeyException($key);
}

/**
* (non-PHPdoc)
* @see \Circle\UtilityBundle\Interfaces\Container::contains()
*/
public function contains($element) {
return $this->collection->contains($element);
}

/**
* (non-PHPdoc)
* @see \Circle\UtilityBundle\Interfaces\Container::toArray()
Expand All @@ -56,7 +62,7 @@ public function toArray() {
/**
* checks if $key exists
*
* @param string $key
* @param string $key
* @return boolean
*/
private function has($key) {
Expand Down
8 changes: 8 additions & 0 deletions Interfaces/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@ public function get($key);
* @return array
*/
public function toArray();

/**
* returns if the container contains the given element
*
* @param mixed $element
* @return bool
*/
public function contains($element);
}
9 changes: 5 additions & 4 deletions Interfaces/Reflectable.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*/
namespace Circle\UtilityBundle\Interfaces;

use Doctrine\Common\Collections\ArrayCollection;
use Circle\UtilityBundle\Interfaces\Container;

/**
*
* a type that can give information
Expand Down Expand Up @@ -63,7 +64,7 @@ public function exists($property);
* returns the reflection class of
* this object
*
* @return ReflectionClass
* @return \ReflectionClass
*/
public function reflect();

Expand All @@ -72,7 +73,7 @@ public function reflect();
* returns all methods of this class
* as reflectionmethods
*
* @return Circle\UtilityBundle\Container\Container
* @return Container
*/
public function getMethods();

Expand All @@ -81,7 +82,7 @@ public function getMethods();
* returns all properties of this class
* as reflectionproperties
*
* @return Circle\UtilityBundle\Container\Container
* @return Container
*/
public function getProperties();
}
11 changes: 11 additions & 0 deletions Tests/Container/ImmutableContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,15 @@ public function toArray(Container $container) {
"test6" => "test"
), $container->toArray());
}

/**
* @test
* @group small
* @covers ::contains
* @depends construct
*/
public function containsElement(Container $container) {
$this->assertTrue($container->contains(2.0));
$this->assertFalse($container->contains('doesNotContain'));
}
}
38 changes: 38 additions & 0 deletions Tests/Traits/ReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,42 @@ public function getMethods() {
public function getProperties() {
$this->assertInstanceOf("Circle\UtilityBundle\Container\ImmutableContainer", $this->createOne()->getProperties());
}

/**
* @test
* @group small
* @covers ::getTraits
* @covers Circle\UtilityBundle\Traits\ReflectionPriv::_getTraits
* @covers Circle\UtilityBundle\Traits\ReflectionPriv::_getAllTraits
* @depends reflect
* @uses Circle\UtilityBundle\Container\ImmutableContainer
*/
public function getTraits() {
$this->assertInstanceOf("Circle\UtilityBundle\Container\ImmutableContainer", $this->createOne()->getTraits());
}

/**
* @test
* @group small
* @covers ::getTraitNames
* @covers Circle\UtilityBundle\Traits\ReflectionPriv::_getTraitNames
* @covers Circle\UtilityBundle\Traits\ReflectionPriv::_getAllTraitNames
* @depends reflect
* @uses Circle\UtilityBundle\Container\ImmutableContainer
*/
public function getTraitNames() {
$this->assertInstanceOf("Circle\UtilityBundle\Container\ImmutableContainer", $this->createOne()->getTraitNames());
}

/**
* @test
* @group small
* @covers ::getInterfaceNames
* @covers Circle\UtilityBundle\Traits\ReflectionPriv::_getInterfaceNames
* @depends reflect
* @uses Circle\UtilityBundle\Container\ImmutableContainer
*/
public function getInterfaceNames() {
$this->assertInstanceOf("Circle\UtilityBundle\Container\ImmutableContainer", $this->createOne()->getInterfaceNames());
}
}
27 changes: 26 additions & 1 deletion Traits/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public function className() {
* (non-PHPdoc)
* @see \Circle\UtilityBundle\Traits\Reflection::_exists()
*
* @param string $property
* @param string $property
* @return bool
*/
public function exists($property) {
return $this->_exists($property);
Expand Down Expand Up @@ -81,4 +82,28 @@ public function getMethods() {
public function getProperties() {
return $this->_getProperties();
}

/**
* (non-PHPdoc)
* @see \Circle\UtilityBundle\Traits\Reflection::_getTraits()
*/
public function getTraits() {
return $this->_getTraits();
}

/**
* (non-PHPdoc)
* @see \Circle\UtilityBundle\Traits\Reflection::_getTraitNames()
*/
public function getTraitNames() {
return $this->_getTraitNames();
}

/**
* (non-PHPdoc)
* @see \Circle\UtilityBundle\Traits\Reflection::_getInterfaceNames()
*/
public function getInterfaceNames($deep = false) {
return $this->_getInterfaceNames($deep);
}
}
75 changes: 74 additions & 1 deletion Traits/ReflectionPriv.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
* @copyright TeeAge-Beatz UG 2014
*/
trait ReflectionPriv {

/**
* @var \ReflectionClass
*/
private $reflection;

/**
* returns the alias string of the using
* object
Expand Down Expand Up @@ -85,7 +91,7 @@ private function _exists($property) {
* @return \ReflectionClass
*/
private function _reflect() {
return new \ReflectionClass($this);
return $this->reflection = empty($this->reflection) ? new \ReflectionClass($this) : $this->reflection;
}

/**
Expand All @@ -103,6 +109,8 @@ private function _toAlias($className) {
/**
* returns all methods of this class wrapped
* in an immutable container
*
* @SuppressWarnings("PHPMD.StaticAccess");
*
* @return Circle\UtilityBundle\Container\ImmutableContainer
*/
Expand All @@ -114,9 +122,74 @@ private function _getMethods() {
* returns all properties of this class wrapped
* in an immutable container
*
* @SuppressWarnings("PHPMD.StaticAccess");
*
* @return Circle\UtilityBundle\Container\ImmutableContainer
*/
private function _getProperties() {
return new ImmutableContainer($this->_reflect()->getProperties());
}

/**
* returns all traits of this class wrapped
* in an immutable container
*
* @SuppressWarnings("PHPMD.StaticAccess");
*
* @return ImmutableContainer
*/
private function _getTraits() {
return new ImmutableContainer($this->_getAllTraits($this->_reflect()));
}

/**
* returns all traits names of this class wrapped
* in an immutable container
*
* @SuppressWarnings("PHPMD.StaticAccess");
*
* @return ImmutableContainer
*/
private function _getTraitNames() {
return new ImmutableContainer($this->_getAllTraitNames($this->_reflect()));
}

/**
* returns all trait names of this class and all
* trait names used in the traits used by the class
*
* @param \ReflectionClass $class
* @param string[] $traits
* @return string[]
*/
private function _getAllTraitNames(\ReflectionClass $class, array $traits = array()) {
foreach ($class->getTraits() as $trait) $traits = $this->_getAllTraitNames($trait, $traits);
$traits = $class->getParentClass() instanceof \ReflectionClass ? $this->_getAllTraitNames($class->getParentClass(), $traits) : $traits;
return $class->isTrait() ? array_merge($traits, array($class->getName())) : $traits;
}

/**
* returns all traits of this class and all
* traits used in the traits used by the class
*
* @param \ReflectionClass $class
* @param \ReflectionClass[] $traits
* @return \ReflectionClass[]
*/
private function _getAllTraits(\ReflectionClass $class, array $traits = array()) {
foreach ($class->getTraits() as $trait) $traits = $this->_getAllTraits($trait, $traits);
$traits = $class->getParentClass() instanceof \ReflectionClass ? $this->_getAllTraits($class->getParentClass(), $traits) : $traits;
return $class->isTrait() ? array_merge($traits, array($class)) : $traits;
}

/**
* returns all interface names
*
* @SuppressWarnings("PHPMD.StaticAccess");
*
* @return ImmutableContainer
*/
private function _getInterfaceNames() {
return new ImmutableContainer($this->_reflect()->getInterfaceNames());
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"target-dir" : "Circle/UtilityBundle",
"extra" : {
"branch-alias" : {
"dev-master" : "1.1-dev"
"dev-master" : "1.2-dev"
}
}
}

0 comments on commit 5c231cc

Please sign in to comment.