Completed
Push — develop ( 2e4b7c...0790da )
by Stuart
05:52
created

class-object-functions.php ➔ has_class_props()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 28
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 11
c 1
b 0
f 1
nc 5
nop 2
dl 0
loc 28
rs 8.439
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/ArrayFunctions
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
/**
45
 * get a class's static properties
46
 *
47
 * @param  string $target
48
 *         the class to examine
49
 * @param  int $filter
50
 *         the kind of properties to look for
51
 *         default is to look for public properties only
52
 * @return array
53
 *         a (possibly empty) read-only list of the class's static properties
54
 * @throws InvalidArgumentException
55
 *         if $target is not a valid class name
56
 */
57
function get_class_props($target, $filter = ReflectionProperty::IS_PUBLIC)
58
{
59
    // robustness!!
60
    if (!is_stringy($target)) {
61
        throw new InvalidArgumentException('$target is not a string, is a ' . get_printable_type($target));
62
    }
63
64
    // make sure we have a valid class
65
    if (!class_exists($target) && !interface_exists($target)) {
66
        throw new InvalidArgumentException("class/interface '" . $target . "' not found");
67
    }
68
69
    // use PHP reflection to get the most accurate results
70
    $refObj = new ReflectionClass($target);
71
    $refProps = $refObj->getProperties($filter + ReflectionProperty::IS_STATIC);
72
73
    // what did we get?
74
    $retval = [];
75
    foreach ($refProps as $refProp) {
76
        // ReflectionClass::getProperties() will return non-static properties
77
        // too, and we have to filter them out manually :(
78
        if (!$refProp->isStatic()) {
79
            continue;
80
        }
81
        $refProp->setAccessible(true);
82
        $retval[$refProp->getName()] = $refProp->getValue();
83
    }
84
85
    // all done
86
    return $retval;
87
}
88
89
/**
90
 * does a class have static properties?
91
 *
92
 * @param  string $target
93
 *         the class to examine
94
 * @param  int $filter
95
 *         the kind of properties to look for
96
 *         default is to look for public properties only
97
 * @return boolean
98
 *         TRUE if $target is a class with static properties
99
 *         FALSE otherwise
100
 */
101
function has_class_props($target, $filter = ReflectionProperty::IS_PUBLIC)
102
{
103
    // robustness!!
104
    if (!is_stringy($target)) {
105
        throw new InvalidArgumentException('$target is not a string, is a ' . get_printable_type($target));
106
    }
107
108
    // make sure we have a valid class
109
    if (!class_exists($target) && !interface_exists($target)) {
110
        throw new InvalidArgumentException("class/interface '" . $target . "' not found");
111
    }
112
113
    // use PHP reflection to get the most accurate results
114
    $refObj = new ReflectionClass($target);
115
    $refProps = $refObj->getProperties($filter + ReflectionProperty::IS_STATIC);
116
117
    // what did we get?
118
    foreach ($refProps as $refProp) {
119
        // ReflectionClass::getProperties() will return non-static properties
120
        // too, and we have to filter them out manually :(
121
        if ($refProp->isStatic()) {
122
            return true;
123
        }
124
    }
125
126
    // if we get here, then the class has no static properties
127
    return false;
128
}
129
130
/**
131
 * does an object have properties?
132
 *
133
 * @param  object  $target
134
 *         the object to examine
135
 * @param  int $filter
136
 *         the kind of properties to look for
137
 *         default is to look for public properties only
138
 * @return boolean
139
 *         TRUE if $target is an object with properties
140
 *         FALSE otherwise
141
 */
142
function has_object_props($target, $filter = ReflectionProperty::IS_PUBLIC)
143
{
144
    // robustness!!
145
    if (!is_object($target)) {
146
        return false;
147
    }
148
149
    // use PHP reflection to get the most accurate results
150
    $refObj = new ReflectionObject($target);
151
    $refProps = $refObj->getProperties($filter);
152
153
    // what did we get?
154
    if (!is_array($refProps) || count($refProps) === 0) {
155
        return false;
156
    }
157
    
158
    // ReflectionObject::getProperties() will return static properties too :(
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
159
    foreach ($refProps as $refProp) {
160
        if (!$refProp->isStatic()) {
161
            return true;
162
        }
163
    }
164
165
    return false;
166
}
167