/** @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);
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();
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);
*/
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);
public function testParserMethodExplicit()
{
- $reader = new Reader(SomeClass::class, 'parserFixture', 'method');
+ $reader = new Reader(SomeClass::class, 'parserFixture', Reader::TYPE_METHOD);
$this->commonTest($reader);
}
}
- public function testParseFunction()
+ public function testParseFunctionImplicit()
{
include_once __DIR__ . "/model/SomeMethod.php";
$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');
public function testConstantParser()
{
- $reader = new Reader(SomeClass::class, "MY_CONST", "constant");
+ $reader = new Reader(SomeClass::class, "MY_CONST", Reader::TYPE_CONSTANT);
$parameters = $reader->getParameters();
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);
}