Completed
Push — develop ( f80003...852d20 )
by Stuart
02:27
created

HasClassProperties::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 GanbaroDigital\MissingBits\Checks\Check;
47
use GanbaroDigital\MissingBits\Checks\ListCheck;
48
use GanbaroDigital\MissingBits\Checks\ListCheckHelper;
49
use InvalidArgumentException;
50
use ReflectionClass;
51
use ReflectionProperty;
52
use TypeError;
53
54
/**
55
 * does a class have static properties?
56
 */
57
class HasClassProperties implements Check, ListCheck
58
{
59
    // save us having to implement it ourselves
60
    use ListCheckHelper;
61
62
    /**
63
     * which types of property do we want to check for?
64
     * @var int
65
     */
66
    protected $propTypes;
67
68
    /**
69
     * create a customised HasClassProperties checker, ready to use
70
     *
71
     * @param  int $propTypes
72
     *         the kind of properties to look for
73
     *         default is to look for public properties only
74
     * @return HasClassProperties
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
75
     *         a customised Check, ready to use
76
     */
77
    public function __construct($propTypes = ReflectionProperty::IS_PUBLIC)
78
    {
79
        $this->propTypes = $propTypes;
80
    }
81
82
    /**
83
     * create a customised HasClassProperties checker, ready to use
84
     *
85
     * @param  int $propTypes
86
     *         the kind of properties to look for
87
     *         default is to look for public properties only
88
     * @return HasClassProperties
89
     *         a customised Check, ready to use
90
     */
91
    public static function using($propTypes = ReflectionProperty::IS_PUBLIC)
92
    {
93
        return new static($propTypes);
94
    }
95
96
    /**
97
     * does a class have static properties?
98
     *
99
     * @param  string $target
100
     *         the class to examine
101
     * @return boolean
102
     *         TRUE if the class has static properties
103
     *         FALSE otherwise
104
     * @throws InvalidArgumentException
105
     *         if $target is not a valid class name
106
     */
107
    public function inspect($target)
108
    {
109
        return static::check($target, $this->propTypes);
110
    }
111
112
    /**
113
     * does a class have static properties?
114
     *
115
     * @param  string $target
116
     *         the class to examine
117
     * @param  int $propTypes
118
     *         the kind of properties to look for
119
     *         default is to look for public properties only
120
     * @return boolean
121
     *         TRUE if the class has static properties
122
     *         FALSE otherwise
123
     * @throws InvalidArgumentException
124
     *         if $target is not a valid class name
125
     */
126
    public static function check($target, $propTypes = ReflectionProperty::IS_PUBLIC)
127
    {
128
        // robustness!!
129
        if (!check_is_stringy($target)) {
130
            throw new TypeError('$target is not a string, is a ' . get_printable_type($target));
131
        }
132
133
        // make sure we have a valid class
134
        if (!class_exists($target) && !interface_exists($target)) {
135
            throw new InvalidArgumentException("class/interface '" . $target . "' not found");
136
        }
137
138
        // if we get here, then we want to do this
139
        $refObj = new ReflectionClass($target);
140
        $resultFilter = [IsClassProperty::class, 'check'];
141
        return HasFilteredProperties::check($refObj, $propTypes | ReflectionProperty::IS_STATIC, $resultFilter);
142
    }
143
144
    /**
145
     * does a list of classes have static properties?
146
     *
147
     * @param  mixed $list
148
     *         the list of classes to examine
149
     * @param  int $propTypes
150
     *         the kind of properties to look for
151
     *         default is to look for public properties only
152
     * @return boolean
153
     *         TRUE if the class has static properties
154
     *         FALSE otherwise
155
     * @throws InvalidArgumentException
156
     *         if $target is not a valid class name
157
     */
158
    public static function checkList($list, $propTypes = ReflectionProperty::IS_PUBLIC)
159
    {
160
        $check = new static($propTypes);
161
        return $check->inspectList($list);
162
    }
163
}
164