From cf16f630f2211d388b8d254bf2ea936c232b3cb4 Mon Sep 17 00:00:00 2001 From: Jaapio Date: Thu, 16 Jan 2020 09:49:07 +0100 Subject: [PATCH] Fix code style --- src/DocBlock/Tags/InvalidTag.php | 42 +++++++++++++-------- tests/unit/DocBlock/Tags/InvalidTagTest.php | 28 ++++++++------ 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/DocBlock/Tags/InvalidTag.php b/src/DocBlock/Tags/InvalidTag.php index a3b68d4..a531c03 100644 --- a/src/DocBlock/Tags/InvalidTag.php +++ b/src/DocBlock/Tags/InvalidTag.php @@ -9,6 +9,13 @@ use phpDocumentor\Reflection\DocBlock\Tag; use ReflectionClass; use ReflectionFunction; use Throwable; +use function array_map; +use function array_walk_recursive; +use function get_class; +use function get_resource_type; +use function is_object; +use function is_resource; +use function sprintf; /** * This class represents an exception during the tag creation @@ -77,24 +84,26 @@ final class InvalidTag implements Tag $traceProperty = (new ReflectionClass('Exception'))->getProperty('trace'); $traceProperty->setAccessible(true); - $flatten = static function (&$value) { - if ($value instanceof Closure) { - $closureReflection = new ReflectionFunction($value); - $value = sprintf( - '(Closure at %s:%s)', - $closureReflection->getFileName(), - $closureReflection->getStartLine() - ); - } elseif (is_object($value)) { - $value = sprintf('object(%s)', get_class($value)); - } elseif (is_resource($value)) { - $value = sprintf('resource(%s)', get_resource_type($value)); - } - }; + $flatten = + /** @param mixed $value */ + static function (&$value) : void { + if ($value instanceof Closure) { + $closureReflection = new ReflectionFunction($value); + $value = sprintf( + '(Closure at %s:%s)', + $closureReflection->getFileName(), + $closureReflection->getStartLine() + ); + } elseif (is_object($value)) { + $value = sprintf('object(%s)', get_class($value)); + } elseif (is_resource($value)) { + $value = sprintf('resource(%s)', get_resource_type($value)); + } + }; do { $trace = array_map( - static function($call) use ($flatten) { + static function (array $call) use ($flatten) : array { array_walk_recursive($call['args'], $flatten); return $call; @@ -102,7 +111,8 @@ final class InvalidTag implements Tag $exception->getTrace() ); $traceProperty->setValue($exception, $trace); - } while ($exception = $exception->getPrevious()); + $exception = $exception->getPrevious(); + } while ($exception !== null); $traceProperty->setAccessible(false); } diff --git a/tests/unit/DocBlock/Tags/InvalidTagTest.php b/tests/unit/DocBlock/Tags/InvalidTagTest.php index c562ef3..49d79a6 100644 --- a/tests/unit/DocBlock/Tags/InvalidTagTest.php +++ b/tests/unit/DocBlock/Tags/InvalidTagTest.php @@ -7,6 +7,10 @@ namespace phpDocumentor\Reflection\DocBlock\Tags; use Exception; use InvalidArgumentException; use PHPUnit\Framework\TestCase; +use Throwable; +use function fopen; +use function serialize; +use function unserialize; /** * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\InvalidTag @@ -38,7 +42,7 @@ final class InvalidTagTest extends TestCase self::assertSame('name', $tag->getName()); self::assertSame('@name Body', $tag->render()); - self::assertSame('Body', (string)$tag); + self::assertSame('Body', (string) $tag); self::assertSame($exception, $tag->getException()); } @@ -46,9 +50,9 @@ final class InvalidTagTest extends TestCase { try { $this->throwExceptionFromClosureWithClosureArgument(); - } catch (Exception $e) { + } catch (Throwable $e) { $parentException = new Exception('test', 0, $e); - $tag = InvalidTag::create('Body', 'name')->withError($parentException); + $tag = InvalidTag::create('Body', 'name')->withError($parentException); self::assertSame('name', $tag->getName()); self::assertSame('@name Body', $tag->render()); self::assertSame($parentException, $tag->getException()); @@ -58,36 +62,36 @@ final class InvalidTagTest extends TestCase } } - private function throwExceptionFromClosureWithClosureArgument() + private function throwExceptionFromClosureWithClosureArgument() : void { - $function = function() { + $function = static function () : void { throw new InvalidArgumentException(); }; $function($function); } - public function testCreationWithErrorContainingResource() + public function testCreationWithErrorContainingResource() : void { try { $this->throwExceptionWithResourceArgument(); - } catch (Exception $e) { + } catch (Throwable $e) { $parentException = new Exception('test', 0, $e); - $tag = InvalidTag::create('Body', 'name')->withError($parentException); + $tag = InvalidTag::create('Body', 'name')->withError($parentException); self::assertSame('name', $tag->getName()); self::assertSame('@name Body', $tag->render()); self::assertSame($parentException, $tag->getException()); self::assertStringStartsWith( 'resource(stream)', - $tag->getException()->getPrevious()->getTrace()[0]['args'][0]) - ; + $tag->getException()->getPrevious()->getTrace()[0]['args'][0] + ); self::assertEquals($parentException, unserialize(serialize($parentException))); } } - private function throwExceptionWithResourceArgument() + private function throwExceptionWithResourceArgument() : void { - $function = function() { + $function = static function () : void { throw new InvalidArgumentException(); };