OSDN Git Service

refactoring
authorfrostbane <frostbane@programmer.net>
Fri, 15 Nov 2019 06:30:54 +0000 (15:30 +0900)
committerfrostbane <frostbane@programmer.net>
Fri, 15 Nov 2019 06:42:16 +0000 (15:42 +0900)
move constructor to a new function
convert types to constants

src/DocBlockReader/Reader.php
tests/ReaderTest.php

index db54eb5..4d58e53 100644 (file)
@@ -11,21 +11,23 @@ class Reader
     /** @var bool */
     private $parsedAll = false;
 
-    const keyPattern = '[A-z0-9\_\-]+';
-    const endPattern = "[ ]*(?:@|\r\n|\n)";
+    private const keyPattern = '[A-z0-9\_\-]+';
+    private const endPattern = "[ ]*(?:@|\r\n|\n)";
 
-    public function __construct($class, $method = null, $type = null)
-    {
-        if (is_array($class)) {
-            $method = $class[1];
-            $class  = $class[0];
-            $type   = "method";
-        }
+    public const TYPE_METHOD   = "* method";
+    public const TYPE_PROPERTY = "* property";
+    public const TYPE_CONSTANT = "* constant";
+    public const TYPE_FUNCTION = "* function";
 
-        $arguments = array($class);
-
-        $method !== null and $arguments[] = $method;
-        $type !== null and $arguments[] = $type;
+    /**
+     * @param $arguments
+     *
+     * @return \ReflectionClass|\ReflectionClassConstant|\ReflectionFunction|\ReflectionMethod|\ReflectionProperty
+     * @throws \ReflectionException
+     */
+    private function create($arguments)
+    {
+        @list($class, $method, $type) = $arguments;
 
         $count = count($arguments);
 
@@ -34,22 +36,61 @@ class Reader
         if ($count === 0) {
             throw new \DomainException("No zero argument constructor allowed");
         } else if ($count === 1) {
-            if (is_string($arguments[0]) && function_exists($arguments[0])) {
-                $reflection = new \ReflectionFunction($arguments[0]);
+            if (is_string($class) && function_exists($class)) {
+                return new \ReflectionFunction($class);
             } else {
-                $reflection = new \ReflectionClass($arguments[0]);
+                return new \ReflectionClass($class);
             }
         } else {
-            $type = $count === 3 ? $arguments[2] : "method";
-
-            if ($type === "method") {
-                $reflection = new \ReflectionMethod($arguments[0], $arguments[1]);
-            } else if ($type === "property") {
-                $reflection = new \ReflectionProperty($arguments[0], $arguments[1]);
-            } else if ($type === "constant") {
-                $reflection = new \ReflectionClassConstant($arguments[0], $arguments[1]);
+            if ($count === 2) {
+                if ($method === self::TYPE_FUNCTION) {
+                    return new \ReflectionFunction($class);
+                }
+
+                $type = self::TYPE_METHOD;
+            }
+
+            if ($type === self::TYPE_METHOD) {
+                return new \ReflectionMethod($class, $method);
+            } else if ($type === self::TYPE_PROPERTY) {
+                return new \ReflectionProperty($class, $method);
+            } else if ($type === self::TYPE_CONSTANT) {
+                return new \ReflectionClassConstant($class, $method);
             }
         }
+    }
+
+    /**
+     * Reader constructor.
+     *
+     * $class could be one of the following
+     *
+     * - callable
+     * - class name
+     * - class instance
+     *
+     * throws \ReflectionException
+     * (@ throws annotation is implicitly defined)
+     *
+     * @param mixed       $class
+     * @param null|string $method
+     * @param null|string $type
+     *
+     */
+    public function __construct($class, ?string $method = null, ?string $type = null)
+    {
+        if (is_array($class)) {
+            $method = $class[1];
+            $class  = $class[0];
+            $type   = self::TYPE_METHOD;
+        }
+
+        $arguments = array($class);
+
+        $method !== null and $arguments[] = $method;
+        $type !== null and $arguments[] = $type;
+
+        $reflection = $this->create($arguments);
 
         $this->rawDocBlock = $reflection->getDocComment();
         $this->parameters  = array();
index 6257dc8..42fca69 100644 (file)
@@ -24,13 +24,13 @@ class ReaderTest extends TestCase
 
     public function testPropertyParsing()
     {
-        $reader = new Reader(SomeClass::class, 'myVar', 'property');
+        $reader = new Reader(SomeClass::class, 'myVar', Reader::TYPE_PROPERTY);
         $this->commonTest($reader);
     }
 
     public function testPropertyParsing2()
     {
-        $reader = new Reader(SomeClass::class, 'myVar2', 'property');
+        $reader = new Reader(SomeClass::class, 'myVar2', Reader::TYPE_PROPERTY);
         $x      = $reader->getParameter("x");
         $y      = $reader->getParameter("y");
         $this->assertSame(1, $x);
@@ -45,7 +45,7 @@ class ReaderTest extends TestCase
      */
     public function testIssue2Problem()
     {
-        $reader = new Reader(SomeClass::class, 'issue2', 'property');
+        $reader = new Reader(SomeClass::class, 'issue2', Reader::TYPE_PROPERTY);
         $Lalala = $reader->getParameters()["Lalala"];
 
         $this->assertSame(array("somejsonarray", "2", "anotherjsonarray", "3"), $Lalala);
@@ -59,7 +59,7 @@ class ReaderTest extends TestCase
 
     public function testParserMethodExplicit()
     {
-        $reader = new Reader(SomeClass::class, 'parserFixture', 'method');
+        $reader = new Reader(SomeClass::class, 'parserFixture', Reader::TYPE_METHOD);
         $this->commonTest($reader);
     }
 
@@ -245,7 +245,7 @@ class ReaderTest extends TestCase
 
     }
 
-    public function testParseFunction()
+    public function testParseFunctionImplicit()
     {
         include_once __DIR__ . "/model/SomeMethod.php";
 
@@ -258,6 +258,19 @@ class ReaderTest extends TestCase
         $this->commonTest($reader);
     }
 
+    public function testParseFunctionExplict()
+    {
+        include_once __DIR__ . "/model/SomeMethod.php";
+
+        $method = "\\frostbane\\DocBlockReader\\test\\model\\someMethod";
+
+        $this->assertTrue(function_exists($method));
+
+        $reader = new Reader($method, Reader::TYPE_FUNCTION);
+
+        $this->commonTest($reader);
+    }
+
     public function testFive()
     {
         $reader1 = new Reader(SomeClass::class, 'fixtureFive');
@@ -306,7 +319,7 @@ class ReaderTest extends TestCase
 
     public function testConstantParser()
     {
-        $reader = new Reader(SomeClass::class, "MY_CONST", "constant");
+        $reader = new Reader(SomeClass::class, "MY_CONST", Reader::TYPE_CONSTANT);
 
         $parameters = $reader->getParameters();
 
@@ -315,7 +328,7 @@ class ReaderTest extends TestCase
 
     public function testConstantParserCommon()
     {
-        $reader = new Reader(SomeClass::class, "MY_OTHER_CONST", "constant");
+        $reader = new Reader(SomeClass::class, "MY_OTHER_CONST", Reader::TYPE_CONSTANT);
 
         $this->commonTest($reader);
     }