1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under AGPL, Commercial license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/addendum |
7
|
|
|
* @licence AGPL, Commercial |
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes) |
9
|
|
|
* @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes) |
10
|
|
|
* @copyright Copyright (c) Jan Suchal (Original version, builder, parser) |
11
|
|
|
* @link http://maslosoft.com/addendum/ - maslosoft addendum |
12
|
|
|
* @link https://code.google.com/p/addendum/ - original addendum project |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Maslosoft\Addendum; |
16
|
|
|
|
17
|
|
|
use Maslosoft\Addendum\Exceptions\CircularReferenceException; |
18
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
19
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotationInterface; |
20
|
|
|
use Maslosoft\Addendum\Utilities\ConflictChecker; |
21
|
|
|
use Maslosoft\Addendum\Utilities\TargetChecker; |
22
|
|
|
use ReflectionClass; |
23
|
|
|
use ReflectionProperty; |
24
|
|
|
use UnexpectedValueException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Modernized Addendum PHP Reflection Annotations |
28
|
|
|
* http://code.google.com/p/addendum/ |
29
|
|
|
* |
30
|
|
|
* Copyright (C) 2006-2009 Jan "johno Suchal <[email protected]> |
31
|
|
|
|
32
|
|
|
* This library is free software; you can redistribute it and/or |
33
|
|
|
* modify it under the terms of the GNU Lesser General Public |
34
|
|
|
* License as published by the Free Software Foundation; either |
35
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
36
|
|
|
|
37
|
|
|
* This library is distributed in the hope that it will be useful, |
38
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
39
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
40
|
|
|
* Lesser General Public License for more details. |
41
|
|
|
|
42
|
|
|
* You should have received a copy of the GNU Lesser General Public |
43
|
|
|
* License along with this library; if not, write to the Free Software |
44
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
45
|
|
|
* */ |
46
|
|
|
abstract class Annotation implements AnnotationInterface |
47
|
|
|
{ |
48
|
|
|
|
49
|
|
|
use Traits\MetaState; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* This is annotated class instance, must be set before calling init |
53
|
|
|
* @var AnnotatedInterface |
54
|
|
|
*/ |
55
|
|
|
protected $_component; |
56
|
|
|
protected $_properties = []; |
57
|
|
|
protected $_publicProperties = []; |
58
|
|
|
private static $_creationStack = []; |
59
|
|
|
|
60
|
46 |
|
public function __construct($data = [], $target = false) |
61
|
|
|
{ |
62
|
46 |
|
$reflection = new ReflectionClass($this); |
63
|
46 |
|
$class = $reflection->name; |
64
|
46 |
|
if (isset(self::$_creationStack[$class])) |
65
|
46 |
|
{ |
66
|
|
|
throw new CircularReferenceException("Circular annotation reference on '$class'", E_USER_ERROR); |
67
|
|
|
} |
68
|
46 |
|
self::$_creationStack[$class] = true; |
69
|
46 |
|
foreach ($data as $key => $value) |
70
|
|
|
{ |
71
|
39 |
|
if ($reflection->hasProperty($key)) |
72
|
39 |
|
{ |
73
|
39 |
|
$this->$key = $value; |
74
|
39 |
|
} |
75
|
39 |
|
$this->_properties[$key] = $value; |
76
|
46 |
|
} |
77
|
46 |
|
foreach ($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $field) |
78
|
|
|
{ |
79
|
46 |
|
$this->_publicProperties[] = $field->name; |
80
|
46 |
|
} |
81
|
|
|
try |
82
|
|
|
{ |
83
|
46 |
|
ConflictChecker::register($this, $target); |
|
|
|
|
84
|
46 |
|
TargetChecker::check($this, $target); |
85
|
|
|
} |
86
|
46 |
|
catch (UnexpectedValueException $ex) |
87
|
|
|
{ |
88
|
5 |
|
throw $ex; |
89
|
46 |
|
} finally |
90
|
|
|
{ |
91
|
46 |
|
unset(self::$_creationStack[$class]); |
92
|
|
|
} |
93
|
46 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set working component instance |
97
|
|
|
* @param object $component |
98
|
|
|
*/ |
99
|
22 |
|
public function setComponent($component) |
100
|
1 |
|
{ |
101
|
22 |
|
$this->_component = $component; |
102
|
22 |
|
} |
103
|
|
|
|
104
|
|
|
public function getProperties() |
105
|
|
|
{ |
106
|
|
|
return $this->_properties; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Init annoattion |
111
|
|
|
*/ |
112
|
|
|
abstract public function init(); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Convert to array |
116
|
|
|
* @return mixed[] |
117
|
|
|
*/ |
118
|
|
|
public function toArray() |
119
|
|
|
{ |
120
|
|
|
$result = []; |
121
|
|
|
foreach ($this->_publicProperties as $field) |
122
|
|
|
{ |
123
|
|
|
$result[$field] = $this->$field; |
124
|
|
|
} |
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.