Completed
Push — develop ( 13b4a5...81ae19 )
by Stuart
05:22
created

FilterClassProperties   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 57
rs 10
wmc 7
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
C from() 0 41 7
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
50
/**
51
 * get a class's static properties
52
 */
53
class FilterClassProperties
54
{
55
    /**
56
     * get a class's static properties
57
     *
58
     * @param  string $target
59
     *         the class to examine
60
     * @param  int $filter
61
     *         the kind of properties to look for
62
     *         default is to look for public properties only
63
     * @return array
64
     *         a (possibly empty) read-only list of the class's static properties
65
     * @throws InvalidArgumentException
66
     *         if $target is not a valid class name
67
     */
68
    public static function from($target, $filter = ReflectionProperty::IS_PUBLIC)
69
    {
70
        // robustness!!
71
        if (!check_is_stringy($target)) {
72
            throw new InvalidArgumentException('$target is not a string, is a ' . get_printable_type($target));
73
        }
74
75
        // make sure we have a valid class
76
        if (!class_exists($target) && !interface_exists($target)) {
77
            throw new InvalidArgumentException("class/interface '" . $target . "' not found");
78
        }
79
80
        // we must remove IS_STATIC from the filter mask, otherwise
81
        // our $resultFilter function down below cannot work
82
        if ($filter & ReflectionProperty::IS_STATIC) {
83
            $filter = $filter ^ ReflectionProperty::IS_STATIC;
84
        }
85
86
        // if we get here, then we want to do this
87
        $refObj = new ReflectionClass($target);
88
        $appliedFilter = $filter | ReflectionProperty::IS_STATIC;
89
90
        $resultFilter = function(ReflectionProperty $refProp, &$finalResult) use($filter) {
91
            // filter out the potential object properties
92
            if (!IsClassProperty::check($refProp)) {
93
                return;
94
            }
95
96
            // now filter out any properties that don't match our initial
97
            // filter bitmask
98
            //
99
            // this can happen when $filer = ReflectionProperty::IS_PUBLIC
100
            if (!(int)($refProp->getModifiers() & $filter)) {
101
                return;
102
            }
103
104
            // $refProp->setAccessible(true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
105
            $finalResult[$refProp->getName()] = $refProp->getValue();
106
        };
107
        return FilterProperties::from($refObj, $appliedFilter, $resultFilter);
108
    }
109
}
110