Skip to content

Commit 5bb1ded

Browse files
committed
Use PHP7 code structure, move AnnotationException to dedicated namespace
1 parent f8855d3 commit 5bb1ded

12 files changed

+123
-57
lines changed

src/Annotations/Annotation.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
namespace ElementaryFramework\Annotations;
2424

25+
use ElementaryFramework\Annotations\Exceptions\AnnotationException;
26+
2527
/**
2628
* A general-purpose base class for annotations.
2729
*
@@ -32,16 +34,25 @@ abstract class Annotation implements IAnnotation
3234
{
3335
/**
3436
* Insulation against read-access to undeclared properties
37+
*
38+
* @param string $name The property name.
39+
*
40+
* @throws AnnotationException
3541
*/
36-
public function __get($name)
42+
public function __get(string $name)
3743
{
3844
throw new AnnotationException(\get_class($this) . "::\${$name} is not a valid property name");
3945
}
4046

4147
/**
4248
* Insulation against write-access to undeclared properties
49+
*
50+
* @param string $name The property name.
51+
* @param mixed $value The value to set.
52+
*
53+
* @throws AnnotationException
4354
*/
44-
public function __set($name, $value)
55+
public function __set(string $name, $value)
4556
{
4657
throw new AnnotationException(\get_class($this) . "::\${$name} is not a valid property name");
4758
}
@@ -64,6 +75,9 @@ protected function map(array &$properties, array $indexes)
6475

6576
/**
6677
* Initializes this annotation instance.
78+
*
79+
* @param array $properties The array of annotation properties.
80+
*
6781
* @see IAnnotation
6882
*/
6983
public function initAnnotation(array $properties)

src/Annotations/AnnotationCache.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
namespace ElementaryFramework\Annotations;
2424

25+
use ElementaryFramework\Annotations\Exceptions\AnnotationException;
26+
2527
/**
2628
* This class is responsible for storing and updating parsed annotation-data in PHP files.
2729
*/
@@ -48,7 +50,7 @@ class AnnotationCache
4850
* @param string $root absolute path to the root-folder where cache-files will be stored
4951
* @param int $fileMode file creation mode; defaults to 0777
5052
*/
51-
public function __construct($root, $fileMode = 0777)
53+
public function __construct(string $root, int $fileMode = 0777)
5254
{
5355
$this->_root = $root;
5456
$this->_fileMode = $fileMode;
@@ -61,19 +63,20 @@ public function __construct($root, $fileMode = 0777)
6163
*
6264
* @return bool true if data with the given key has been stored; otherwise false
6365
*/
64-
public function exists($key)
66+
public function exists(string $key): bool
6567
{
6668
return \file_exists($this->_getPath($key));
6769
}
6870

6971
/**
7072
* Caches the given data with the given key.
7173
*
72-
* @param string $key cache key
73-
* @param array $code the source-code to be cached
74+
* @param string $key The cache key.
75+
* @param string $code The source-code to be cached
76+
*
7477
* @throws AnnotationException if file could not be written
7578
*/
76-
public function store($key, $code)
79+
public function store(string $key, string $code)
7780
{
7881
$path = $this->_getPath($key);
7982

@@ -91,40 +94,45 @@ public function store($key, $code)
9194
/**
9295
* Fetches data stored for the given key.
9396
*
94-
* @param string $key cache key
95-
* @return mixed the cached data
97+
* @param string $key The cache key.
98+
*
99+
* @return mixed The cached data.
96100
*/
97-
public function fetch($key)
101+
public function fetch(string $key)
98102
{
99-
return include($this->_getPath($key));
103+
return include $this->_getPath($key);
100104
}
101105

102106
/**
103107
* Returns the timestamp of the last cache update for the given key.
104108
*
105-
* @param string $key cache key
106-
* @return int unix timestamp
109+
* @param string $key The cache key.
110+
*
111+
* @return int unix timestamp.
107112
*/
108-
public function getTimestamp($key)
113+
public function getTimestamp(string $key): int
109114
{
110115
return \filemtime($this->_getPath($key));
111116
}
112117

113118
/**
114-
* Maps a cache-key to the absolute path of a PHP file
119+
* Maps a cache-key to the absolute path of a PHP file.
115120
*
116-
* @param string $key cache key
117-
* @return string absolute path of the PHP file
121+
* @param string $key The cache key.
122+
*
123+
* @return string The absolute path of the PHP file.
118124
*/
119-
private function _getPath($key)
125+
private function _getPath(string $key): string
120126
{
121127
return $this->_root . DIRECTORY_SEPARATOR . $key . '.annotations.php';
122128
}
123129

124130
/**
125-
* @return string absolute path of the folder where cache files are created
131+
* Returns absolute path of the folder where cache files are created.
132+
*
133+
* @return string
126134
*/
127-
public function getRoot()
135+
public function getRoot(): string
128136
{
129137
return $this->_root;
130138
}

src/Annotations/AnnotationFile.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class AnnotationFile
7777
* @param string $path absolute path to php source-file
7878
* @param array $data annotation data (as provided by AnnotationParser)
7979
*/
80-
public function __construct($path, array $data)
80+
public function __construct(string $path, array $data)
8181
{
8282
$this->path = $path;
8383
$this->data = $data;
@@ -98,7 +98,7 @@ public function __construct($path, array $data)
9898
*
9999
* @return string[] of fully-qualified class name, method name
100100
*/
101-
public function resolveMethod($raw_method)
101+
public function resolveMethod(string $raw_method): array
102102
{
103103
list($class, $method) = \explode('::', $raw_method, 2);
104104
return array(\ltrim($this->resolveType($class), '\\'), $method);
@@ -112,7 +112,7 @@ public function resolveMethod($raw_method)
112112
* @return string
113113
* @see http://www.phpdoc.org/docs/latest/for-users/phpdoc/types.html#abnf
114114
*/
115-
public function resolveType($raw_type)
115+
public function resolveType(string $raw_type): string
116116
{
117117
$type_parts = \explode('[]', $raw_type, 2);
118118
$type = $type_parts[0];
@@ -135,7 +135,7 @@ public function resolveType($raw_type)
135135
*
136136
* @return boolean
137137
*/
138-
protected function isSimple($type)
138+
protected function isSimple(string $type): bool
139139
{
140140
return \in_array(\strtolower($type), self::$simpleTypes);
141141
}

src/Annotations/AnnotationManager.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
namespace ElementaryFramework\Annotations;
2424

25+
use ElementaryFramework\Annotations\Exceptions\AnnotationException;
26+
2527
/**
2628
* This class manages the retrieval of Annotations from source code files
2729
*/
@@ -75,14 +77,14 @@ class AnnotationManager
7577
'internal' => false,
7678
'license' => false,
7779
'link' => false,
78-
'method' => 'ElementaryFramework\Annotations\standard\MethodAnnotation',
80+
'method' => 'ElementaryFramework\Annotations\Standard\MethodAnnotation',
7981
'name' => false,
8082
'package' => false,
81-
'param' => 'ElementaryFramework\Annotations\standard\ParamAnnotation',
82-
'property' => 'ElementaryFramework\Annotations\standard\PropertyAnnotation',
83-
'property-read' => 'ElementaryFramework\Annotations\standard\PropertyReadAnnotation',
84-
'property-write' => 'ElementaryFramework\Annotations\standard\PropertyWriteAnnotation',
85-
'return' => 'ElementaryFramework\Annotations\standard\ReturnAnnotation',
83+
'param' => 'ElementaryFramework\Annotations\Standard\ParamAnnotation',
84+
'property' => 'ElementaryFramework\Annotations\Standard\PropertyAnnotation',
85+
'property-read' => 'ElementaryFramework\Annotations\Standard\PropertyReadAnnotation',
86+
'property-write' => 'ElementaryFramework\Annotations\Standard\PropertyWriteAnnotation',
87+
'return' => 'ElementaryFramework\Annotations\Standard\ReturnAnnotation',
8688
'see' => false,
8789
'since' => false,
8890
'source' => false,
@@ -92,11 +94,11 @@ class AnnotationManager
9294
'todo' => false,
9395
'tutorial' => false,
9496
'throws' => false,
95-
'type' => 'ElementaryFramework\Annotations\standard\TypeAnnotation',
97+
'type' => 'ElementaryFramework\Annotations\Standard\TypeAnnotation',
9698
'usage' => 'ElementaryFramework\Annotations\UsageAnnotation',
9799
'stop' => 'ElementaryFramework\Annotations\StopAnnotation',
98100
'uses' => false,
99-
'var' => 'ElementaryFramework\Annotations\standard\VarAnnotation',
101+
'var' => 'ElementaryFramework\Annotations\Standard\VarAnnotation',
100102
'version' => false,
101103
);
102104

@@ -155,7 +157,7 @@ class AnnotationManager
155157
*
156158
* @param string $cacheSeed only needed if using more than one AnnotationManager in the same application
157159
*/
158-
public function __construct($cacheSeed = '')
160+
public function __construct(string $cacheSeed = '')
159161
{
160162
$this->_cacheSeed = $cacheSeed;
161163
$this->_usageAnnotation = new UsageAnnotation();
@@ -185,14 +187,15 @@ public function getParser()
185187
* Member-names in the returned array have the following format: Class, Class::method or Class::$member
186188
*
187189
* @param string $path the path of the source-code file from which to obtain annotation-data.
190+
*
188191
* @return AnnotationFile
189192
*
190193
* @throws AnnotationException if cache is not configured
191194
*
192195
* @see $files
193196
* @see $cache
194197
*/
195-
protected function getAnnotationFile($path)
198+
protected function getAnnotationFile(string $path): AnnotationFile
196199
{
197200
if (!isset($this->files[$path])) {
198201
if ($this->cache === null) {
@@ -231,7 +234,7 @@ protected function getAnnotationFile($path)
231234
*
232235
* @see $registry
233236
*/
234-
public function resolveName($name)
237+
public function resolveName(string $name)
235238
{
236239
if (\strpos($name, '\\') !== false) {
237240
return $name . $this->suffix; // annotation class-name is fully qualified
@@ -258,9 +261,10 @@ public function resolveName($name)
258261
* @param string $member_name Optional member name, e.g. "method" or "$property"
259262
*
260263
* @return IAnnotation[] array of IAnnotation objects for the given class/member/name
264+
*
261265
* @throws AnnotationException for bad annotations
262266
*/
263-
protected function getAnnotations($class_name, $member_type = self::MEMBER_CLASS, $member_name = null)
267+
protected function getAnnotations(string $class_name, string $member_type = self::MEMBER_CLASS, string $member_name = null): array
264268
{
265269
$key = $class_name . ($member_name ? '::' . $member_name : '');
266270

@@ -366,7 +370,7 @@ protected function getAnnotations($class_name, $member_type = self::MEMBER_CLASS
366370
*
367371
* @return bool whether class or trait has the specified member
368372
*/
369-
protected function classHasMember($className, $memberType, $memberName)
373+
protected function classHasMember(string $className, string $memberType, string $memberName): bool
370374
{
371375
if ($memberType === self::MEMBER_METHOD) {
372376
return \method_exists($className, $memberName);
@@ -387,7 +391,7 @@ protected function classHasMember($className, $memberType, $memberName)
387391
*
388392
* @throws AnnotationException if a constraint is violated.
389393
*/
390-
protected function applyConstraints(array $annotations, $member)
394+
protected function applyConstraints(array $annotations, string $member): array
391395
{
392396
$result = array();
393397
$annotationCount = \count($annotations);
@@ -431,7 +435,7 @@ protected function applyConstraints(array $annotations, $member)
431435
*
432436
* @return array The filtered array of annotation objects - may return an empty array
433437
*/
434-
protected function filterAnnotations(array $annotations, $type)
438+
protected function filterAnnotations(array $annotations, string $type): array
435439
{
436440
if (\substr($type, 0, 1) === '@') {
437441
$type = $this->resolveName(\substr($type, 1));
@@ -456,10 +460,12 @@ protected function filterAnnotations(array $annotations, $type)
456460
* Obtain the UsageAnnotation for a given Annotation class
457461
*
458462
* @param string $class The Annotation type class-name
463+
*
459464
* @return UsageAnnotation
465+
*
460466
* @throws AnnotationException if the given class-name is invalid; if the annotation-type has no defined usage
461467
*/
462-
public function getUsage($class)
468+
public function getUsage(string $class): UsageAnnotation
463469
{
464470
if ($class === $this->registry['usage']) {
465471
return $this->_usageAnnotation;
@@ -496,9 +502,10 @@ public function getUsage($class)
496502
* Alternatively, prefixing with "@" invokes name-resolution (allowing you to query by annotation name.)
497503
*
498504
* @return Annotation[] Annotation instances
505+
*
499506
* @throws AnnotationException if a given class-name is undefined
500507
*/
501-
public function getClassAnnotations($class, $type = null)
508+
public function getClassAnnotations($class, string $type = null): array
502509
{
503510
if ($class instanceof \ReflectionClass) {
504511
$class = $class->getName();
@@ -534,9 +541,10 @@ public function getClassAnnotations($class, $type = null)
534541
* Alternatively, prefixing with "@" invokes name-resolution (allowing you to query by annotation name.)
535542
*
536543
* @throws AnnotationException for undefined method or class-name
544+
*
537545
* @return IAnnotation[] list of Annotation objects
538546
*/
539-
public function getMethodAnnotations($class, $method = null, $type = null)
547+
public function getMethodAnnotations($class, string $method = null, string $type = null): array
540548
{
541549
if ($class instanceof \ReflectionClass) {
542550
$class = $class->getName();
@@ -576,7 +584,7 @@ public function getMethodAnnotations($class, $method = null, $type = null)
576584
*
577585
* @throws AnnotationException for undefined class-name
578586
*/
579-
public function getPropertyAnnotations($class, $property = null, $type = null)
587+
public function getPropertyAnnotations($class, string $property = null, string $type = null): array
580588
{
581589
if ($class instanceof \ReflectionClass) {
582590
$class = $class->getName();

src/Annotations/AnnotationParser.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
namespace ElementaryFramework\Annotations;
2424

25+
use ElementaryFramework\Annotations\Exceptions\AnnotationException;
26+
2527
if (!defined('T_TRAIT')) {
2628
define(__NAMESPACE__ . '\\T_TRAIT', -2);
2729
}
@@ -80,9 +82,10 @@ public function __construct(AnnotationManager $manager)
8082
* @param string $path The path of the source file being parsed (for error-reporting only)
8183
*
8284
* @return string PHP source code to construct the Annotations of the given PHP source code
85+
*
8386
* @throws AnnotationException if orphaned Annotations are found at the end of the file
8487
*/
85-
public function parse($source, $path)
88+
public function parse(string $source, string $path): string
8689
{
8790
$index = array();
8891
$traitMethodOverrides = array();
@@ -328,9 +331,12 @@ public function parse($source, $path)
328331
* @param string $path The full path of a PHP source code file
329332
*
330333
* @return string PHP source code to construct the annotations of the given PHP source code
334+
*
331335
* @see AttributeParser::parse()
336+
*
337+
* @throws AnnotationException
332338
*/
333-
public function parseFile($path)
339+
public function parseFile(string $path): string
334340
{
335341
return $this->parse(\file_get_contents($path), $path);
336342
}
@@ -339,11 +345,12 @@ public function parseFile($path)
339345
* Scan a PHP source code comment for annotation data
340346
*
341347
* @param string $str PHP comment containing annotations
348+
*
342349
* @return array PHP source code snippets with annotation initialization arrays
343350
*
344351
* @throws AnnotationException for various run-time errors
345352
*/
346-
protected function findAnnotations($str)
353+
protected function findAnnotations(string $str): array
347354
{
348355
$str = \trim(\preg_replace('/^[\/\*\# \t]+/m', '', $str)) . "\n";
349356
$str = \str_replace("\r\n", "\n", $str);

0 commit comments

Comments
 (0)