Completed
Push — master ( 479d34...76ed5d )
by James
16s queued 14s
created

HooksReader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 72
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A reader() 0 3 1
A instance() 0 9 2
A read() 0 21 4
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
final class HooksReader {
12
13
	/**
14
	 * Reader instance.
15
	 *
16
	 * @var AnnotationReader
17
	 */
18
	private $reader;
19
20
	/**
21
	 * Construct a new HooksReader instance.
22
	 */
23 3
	private function __construct() {
24 3
		$this->reader = new AnnotationReader();
25 3
		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...
26 1
			__DIR__ . '/Annotations.php'
27 2
		);
28 3
	}
29
30
	/**
31
	 * Get the Annotation reader.
32
	 *
33
	 * @return AnnotationReader
34
	 */
35 30
	private function reader() {
36 30
		return $this->reader;
37
	}
38
39
	/**
40
	 * Get the shared reader instance.
41
	 *
42
	 * @return HooksReader
43
	 */
44 30
	static private function instance() {
45 30
		static $instance;
46
47 30
		if ( ! $instance ) {
48 3
			$instance = new HooksReader();
49 2
		}
50
51 30
		return $instance;
52
	}
53
54
	/**
55
	 * Shared implementation of hook reading logic.
56
	 *
57
	 * @param  object   $target    Object to read annotations from.
58
	 * @param  \Closure $condition Whether the annotation should be registered.
59
	 * @return array
60
	 */
61 30
	static public function read( $target, $condition ) {
62 30
		$reader = static::instance()->reader();
0 ignored issues
show
Comprehensibility introduced by
Since Intraxia\Jaxion\Core\HooksReader is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
63 30
		$rmethods = (new ReflectionClass( $target ))->getMethods();
64
65 30
		$hooks = [];
66
67 30
		foreach ( $rmethods as $rmethod ) {
68 30
			foreach ( $reader->getMethodAnnotations( $rmethod ) as $annotation ) {
69 24
				if ( $condition( $annotation ) ) {
70 24
					$hooks[] = [
71 24
						'hook' => $annotation->hook,
72 24
						'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...
73 24
						'priority' => $annotation->priority,
74 24
						'args' => $annotation->args,
75
					];
76 16
				}
77 16
			}
78 16
		}
79
80 24
		return $hooks;
81
	}
82
}
83