1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2016-present Ganbaro Digital Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package MissingBits/ClassesAndObjects |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2016-present Ganbaro Digital Ltd www.ganbarodigital.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://ganbarodigital.github.io/php-the-missing-bits |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace GanbaroDigital\MissingBits\ClassesAndObjects; |
45
|
|
|
|
46
|
|
|
use InvalidArgumentException; |
47
|
|
|
use ReflectionClass; |
48
|
|
|
use ReflectionProperty; |
49
|
|
|
use TypeError; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* get a class's static properties |
53
|
|
|
*/ |
54
|
|
|
class FilterClassProperties |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* the kind of properties to look for |
58
|
|
|
* |
59
|
|
|
* @var int |
60
|
|
|
*/ |
61
|
|
|
protected $filter; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* creates a new class properties filter, ready to use |
65
|
|
|
* |
66
|
|
|
* @param int $filter |
67
|
|
|
* the kind of properties to look for |
68
|
|
|
* default is to look for public properties only |
69
|
|
|
*/ |
70
|
|
|
public function __construct($filter = ReflectionProperty::IS_PUBLIC) |
71
|
|
|
{ |
72
|
|
|
$this->filter = $filter; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* creates a new class properties filter, ready to use |
77
|
|
|
* |
78
|
|
|
* @param int $filter |
79
|
|
|
* the kind of properties to look for |
80
|
|
|
* default is to look for public properties only |
81
|
|
|
* @return FilterClassProperties |
82
|
|
|
* a customised FilterClassProperties ready to use |
83
|
|
|
*/ |
84
|
|
|
public static function using($filter = ReflectionProperty::IS_PUBLIC) |
85
|
|
|
{ |
86
|
|
|
return new static($filter); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* get a class's static properties |
91
|
|
|
* |
92
|
|
|
* @param string $target |
93
|
|
|
* the class to examine |
94
|
|
|
* @return array |
95
|
|
|
* a (possibly empty) read-only list of the class's static properties |
96
|
|
|
* @throws InvalidArgumentException |
97
|
|
|
* if $target is not a valid class name |
98
|
|
|
*/ |
99
|
|
|
public function filterClassProperties($target) |
100
|
|
|
{ |
101
|
|
|
return self::from($target, $this->filter); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* get a class's static properties |
106
|
|
|
* |
107
|
|
|
* @param string $target |
108
|
|
|
* the class to examine |
109
|
|
|
* @param int $filter |
110
|
|
|
* the kind of properties to look for |
111
|
|
|
* default is to look for public properties only |
112
|
|
|
* @return array |
113
|
|
|
* a (possibly empty) read-only list of the class's static properties |
114
|
|
|
* @throws InvalidArgumentException |
115
|
|
|
* if $target is not a valid class name |
116
|
|
|
*/ |
117
|
|
|
public static function from($target, $filter = ReflectionProperty::IS_PUBLIC) |
118
|
|
|
{ |
119
|
|
|
// robustness!! |
120
|
|
|
if (!check_is_stringy($target)) { |
121
|
|
|
throw new TypeError('$target is not a string, is a ' . get_printable_type($target)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// make sure we have a valid class |
125
|
|
|
if (!class_exists($target) && !interface_exists($target)) { |
126
|
|
|
throw new InvalidArgumentException("class/interface '" . $target . "' not found"); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// we must remove IS_STATIC from the filter mask, otherwise |
130
|
|
|
// our $resultFilter function down below cannot work |
131
|
|
|
if ($filter & ReflectionProperty::IS_STATIC) { |
132
|
|
|
$filter = $filter ^ ReflectionProperty::IS_STATIC; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// if we get here, then we want to do this |
136
|
|
|
$refObj = new ReflectionClass($target); |
137
|
|
|
$appliedFilter = $filter | ReflectionProperty::IS_STATIC; |
138
|
|
|
|
139
|
|
|
$resultFilter = function(ReflectionProperty $refProp, &$finalResult) use($filter) { |
140
|
|
|
// filter out the potential object properties |
141
|
|
|
if (!IsClassProperty::check($refProp)) { |
142
|
|
|
return; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// now filter out any properties that don't match our initial |
146
|
|
|
// filter bitmask |
147
|
|
|
// |
148
|
|
|
// this can happen when $filer = ReflectionProperty::IS_PUBLIC |
149
|
|
|
if (!(int)($refProp->getModifiers() & $filter)) { |
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// $refProp->setAccessible(true); |
|
|
|
|
154
|
|
|
$finalResult[$refProp->getName()] = $refProp->getValue(); |
155
|
|
|
}; |
156
|
|
|
return FilterProperties::from($refObj, $appliedFilter, $resultFilter); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.