Completed
Pull Request — master (#22)
by James
15:13 queued 02:42
created

HooksReader::reader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Intraxia\Jaxion\Core;
3
4
use Doctrine\Common\Annotations\AnnotationReader;
5
use Doctrine\Common\Annotations\AnnotationRegistry;
6
use ReflectionClass;
7
8
/**
9
 * HooksReader class.
10
 */
11
class HooksReader {
12
13
	/**
14
	 * Construct a new HooksReader instance.
15
	 */
16
	private function __construct() {
17
		$this->reader = new AnnotationReader();
0 ignored issues
show
Bug introduced by
The property reader does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
		AnnotationRegistry::registerFile(
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Common\Annotati...egistry::registerFile() has been deprecated with message: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists')

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
19
			__DIR__ . '/Annotations.php'
20
		);
21
	}
22
23
	/**
24
	 * Get the Annotation reader.
25
	 *
26
	 * @return AnnotationReader
27
	 */
28
	private function reader() {
29
		return $this->reader;
30
	}
31
32
	/**
33
	 * Get the shared reader instance.
34
	 *
35
	 * @return HooksReader
36
	 */
37
	static private function instance() {
38
		static $instance;
39
40
		if ( ! $instance ) {
41
			$instance = new HooksReader();
42
		}
43
44
		return $instance;
45
	}
46
47
	/**
48
	 * Shared implementation of hook reading logic.
49
	 *
50
	 * @param  object   $target    Object to read annotations from.
51
	 * @param  \Closure $condition Whether the annotation should be registered.
52
	 * @return array
53
	 */
54
	static public function read( $target, $condition ) {
55
		$reader = static::instance()->reader();
0 ignored issues
show
Bug introduced by
Since instance() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of instance() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
56
		$rmethods = (new ReflectionClass( $target ))->getMethods();
57
58
		$hooks = [];
59
60
		foreach ( $rmethods as $rmethod ) {
61
			foreach ( $reader->getMethodAnnotations( $rmethod ) as $annotation ) {
62
				if ( $condition( $annotation ) ) {
63
					$hooks[] = [
64
						'hook' => $annotation->hook,
65
						'method' => $rmethod->getName(),
0 ignored issues
show
Bug introduced by
Consider using $rmethod->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
66
						'priority' => $annotation->priority,
67
						'args' => $annotation->args,
68
					];
69
				}
70
			}
71
		}
72
73
		return $hooks;
74
	}
75
}
76