From c48fdb94c6c778298f55f5a0ded3f883ca8b98d1 Mon Sep 17 00:00:00 2001 From: Tobias Hauck Date: Sat, 13 Jun 2015 14:02:03 +0200 Subject: [PATCH 1/5] added reflection functionality --- Container/ImmutableContainer.php | 11 ++--- Tests/Traits/ReflectionTest.php | 40 +++++++++++++++++ Traits/Reflection.php | 27 +++++++++++- Traits/ReflectionPriv.php | 76 +++++++++++++++++++++++++++++++- 4 files changed, 147 insertions(+), 7 deletions(-) diff --git a/Container/ImmutableContainer.php b/Container/ImmutableContainer.php index cef12fa..10f86d0 100644 --- a/Container/ImmutableContainer.php +++ b/Container/ImmutableContainer.php @@ -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); @@ -39,7 +39,8 @@ public function __construct(array $array) { * (non-PHPdoc) * @see \Circle\UtilityBundle\Interfaces\Container::get() * - * @param string $key + * @param string $key + * @return mixed */ public function get($key) { return $this->has($key) ? $this->collection->get($key) : $this->_noKeyException($key); @@ -56,7 +57,7 @@ public function toArray() { /** * checks if $key exists * - * @param string $key + * @param string $key * @return boolean */ private function has($key) { diff --git a/Tests/Traits/ReflectionTest.php b/Tests/Traits/ReflectionTest.php index 5bd5e16..71a5d49 100644 --- a/Tests/Traits/ReflectionTest.php +++ b/Tests/Traits/ReflectionTest.php @@ -114,4 +114,44 @@ 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()); + $this->assertInstanceOf("Circle\UtilityBundle\Container\ImmutableContainer", $this->createOne()->getTraits(true)); + } + + /** + * @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()); + $this->assertInstanceOf("Circle\UtilityBundle\Container\ImmutableContainer", $this->createOne()->getTraitNames(true)); + } + + /** + * @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()); + } } \ No newline at end of file diff --git a/Traits/Reflection.php b/Traits/Reflection.php index f2e45f0..80d2e69 100644 --- a/Traits/Reflection.php +++ b/Traits/Reflection.php @@ -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); @@ -81,4 +82,28 @@ public function getMethods() { public function getProperties() { return $this->_getProperties(); } + + /** + * (non-PHPdoc) + * @see \Circle\UtilityBundle\Traits\Reflection::_getTraits() + */ + public function getTraits($deep = false) { + return $this->_getTraits($deep); + } + + /** + * (non-PHPdoc) + * @see \Circle\UtilityBundle\Traits\Reflection::_getTraitNames() + */ + public function getTraitNames($deep = false) { + return $this->_getTraitNames($deep); + } + + /** + * (non-PHPdoc) + * @see \Circle\UtilityBundle\Traits\Reflection::_getInterfaceNames() + */ + public function getInterfaceNames($deep = false) { + return $this->_getInterfaceNames($deep); + } } \ No newline at end of file diff --git a/Traits/ReflectionPriv.php b/Traits/ReflectionPriv.php index 5bc5e62..e2a957b 100644 --- a/Traits/ReflectionPriv.php +++ b/Traits/ReflectionPriv.php @@ -34,6 +34,12 @@ * @copyright TeeAge-Beatz UG 2014 */ trait ReflectionPriv { + + /** + * @var \ReflectionClass + */ + private $reflection; + /** * returns the alias string of the using * object @@ -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; } /** @@ -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 */ @@ -114,9 +122,75 @@ 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"); + * + * @param bool $deep + * @return ImmutableContainer + */ + private function _getTraits($deep) { + return new ImmutableContainer($deep ? $this->_getAllTraits($this->_reflect()) : $this->_reflect()->getTraits()); + } + + /** + * returns all traits names of this class wrapped + * in an immutable container + * + * @SuppressWarnings("PHPMD.StaticAccess"); + * + * @param bool $deep + * @return ImmutableContainer + */ + private function _getTraitNames($deep) { + return new ImmutableContainer($deep ? $this->_getAllTraitNames($this->_reflect()) : $this->_reflect()->getTraitNames()); + } + + /** + * 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); + 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) : $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()); + } } \ No newline at end of file From 5c19fe2ee86223031abbb12351df483ae38b36fd Mon Sep 17 00:00:00 2001 From: Tobias Hauck Date: Sat, 13 Jun 2015 17:01:42 +0200 Subject: [PATCH 2/5] removed deep flag from getTraits --- Tests/Traits/ReflectionTest.php | 2 -- Traits/Reflection.php | 8 ++++---- Traits/ReflectionPriv.php | 13 ++++++------- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/Tests/Traits/ReflectionTest.php b/Tests/Traits/ReflectionTest.php index 71a5d49..b6a236d 100644 --- a/Tests/Traits/ReflectionTest.php +++ b/Tests/Traits/ReflectionTest.php @@ -126,7 +126,6 @@ public function getProperties() { */ public function getTraits() { $this->assertInstanceOf("Circle\UtilityBundle\Container\ImmutableContainer", $this->createOne()->getTraits()); - $this->assertInstanceOf("Circle\UtilityBundle\Container\ImmutableContainer", $this->createOne()->getTraits(true)); } /** @@ -140,7 +139,6 @@ public function getTraits() { */ public function getTraitNames() { $this->assertInstanceOf("Circle\UtilityBundle\Container\ImmutableContainer", $this->createOne()->getTraitNames()); - $this->assertInstanceOf("Circle\UtilityBundle\Container\ImmutableContainer", $this->createOne()->getTraitNames(true)); } /** diff --git a/Traits/Reflection.php b/Traits/Reflection.php index 80d2e69..50764f4 100644 --- a/Traits/Reflection.php +++ b/Traits/Reflection.php @@ -87,16 +87,16 @@ public function getProperties() { * (non-PHPdoc) * @see \Circle\UtilityBundle\Traits\Reflection::_getTraits() */ - public function getTraits($deep = false) { - return $this->_getTraits($deep); + public function getTraits() { + return $this->_getTraits(); } /** * (non-PHPdoc) * @see \Circle\UtilityBundle\Traits\Reflection::_getTraitNames() */ - public function getTraitNames($deep = false) { - return $this->_getTraitNames($deep); + public function getTraitNames() { + return $this->_getTraitNames(); } /** diff --git a/Traits/ReflectionPriv.php b/Traits/ReflectionPriv.php index e2a957b..da6ae20 100644 --- a/Traits/ReflectionPriv.php +++ b/Traits/ReflectionPriv.php @@ -136,11 +136,10 @@ private function _getProperties() { * * @SuppressWarnings("PHPMD.StaticAccess"); * - * @param bool $deep * @return ImmutableContainer */ - private function _getTraits($deep) { - return new ImmutableContainer($deep ? $this->_getAllTraits($this->_reflect()) : $this->_reflect()->getTraits()); + private function _getTraits() { + return new ImmutableContainer($this->_getAllTraits($this->_reflect())); } /** @@ -149,11 +148,10 @@ private function _getTraits($deep) { * * @SuppressWarnings("PHPMD.StaticAccess"); * - * @param bool $deep * @return ImmutableContainer */ - private function _getTraitNames($deep) { - return new ImmutableContainer($deep ? $this->_getAllTraitNames($this->_reflect()) : $this->_reflect()->getTraitNames()); + private function _getTraitNames() { + return new ImmutableContainer($this->_getAllTraitNames($this->_reflect())); } /** @@ -166,6 +164,7 @@ private function _getTraitNames($deep) { */ 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; } @@ -179,7 +178,7 @@ private function _getAllTraitNames(\ReflectionClass $class, array $traits = arra */ 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) : $traits; + $traits = $class->getParentClass() instanceof \ReflectionClass ? $this->_getAllTraits($class->getParentClass(), $traits) : $traits; return $class->isTrait() ? array_merge($traits, array($class)) : $traits; } From 60dd27097322ccef82803a5dc1937fab0cfe51d3 Mon Sep 17 00:00:00 2001 From: Tobias Hauck Date: Sat, 13 Jun 2015 17:25:09 +0200 Subject: [PATCH 3/5] added contains to container --- Container/ImmutableContainer.php | 11 ++++++++--- Interfaces/Container.php | 8 ++++++++ Tests/Container/ImmutableContainerTest.php | 11 +++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Container/ImmutableContainer.php b/Container/ImmutableContainer.php index 10f86d0..000c829 100644 --- a/Container/ImmutableContainer.php +++ b/Container/ImmutableContainer.php @@ -38,14 +38,19 @@ public function __construct(array $array) { /** * (non-PHPdoc) * @see \Circle\UtilityBundle\Interfaces\Container::get() - * - * @param string $key - * @return mixed */ 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() diff --git a/Interfaces/Container.php b/Interfaces/Container.php index 84906e3..bf16171 100644 --- a/Interfaces/Container.php +++ b/Interfaces/Container.php @@ -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); } \ No newline at end of file diff --git a/Tests/Container/ImmutableContainerTest.php b/Tests/Container/ImmutableContainerTest.php index f090cc0..b4a4886 100644 --- a/Tests/Container/ImmutableContainerTest.php +++ b/Tests/Container/ImmutableContainerTest.php @@ -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')); + } } \ No newline at end of file From af0f80aac7bade680c0f79e559fd57084428aae0 Mon Sep 17 00:00:00 2001 From: Tobias Hauck Date: Sat, 13 Jun 2015 18:29:06 +0200 Subject: [PATCH 4/5] fixed phpdoc for reflectable --- Interfaces/Reflectable.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Interfaces/Reflectable.php b/Interfaces/Reflectable.php index 19bea6c..40b53ad 100644 --- a/Interfaces/Reflectable.php +++ b/Interfaces/Reflectable.php @@ -20,7 +20,8 @@ */ namespace Circle\UtilityBundle\Interfaces; -use Doctrine\Common\Collections\ArrayCollection; +use Circle\UtilityBundle\Interfaces\Container; + /** * * a type that can give information @@ -63,7 +64,7 @@ public function exists($property); * returns the reflection class of * this object * - * @return ReflectionClass + * @return \ReflectionClass */ public function reflect(); @@ -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(); @@ -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(); } \ No newline at end of file From d4fff68ab88aafa5599c6476b589f4ed046b48cd Mon Sep 17 00:00:00 2001 From: TobiasHauck Date: Sat, 13 Jun 2015 19:29:31 +0200 Subject: [PATCH 5/5] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6c24617..9406f47 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "target-dir" : "Circle/UtilityBundle", "extra" : { "branch-alias" : { - "dev-master" : "1.1-dev" + "dev-master" : "1.2-dev" } } }