Completed
Push — develop ( 043a76...fdbde9 )
by Stuart
02:39
created

IsObjectOfType   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 78
rs 10
c 1
b 0
f 1
wmc 6
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A check() 0 13 3
A inspect() 0 4 1
A checkList() 0 5 1
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/TypeChecks
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\TypeChecks;
45
46
use GanbaroDigital\MissingBits\Checks\Check;
47
use GanbaroDigital\MissingBits\Checks\ListCheck;
48
use GanbaroDigital\MissingBits\Checks\ListCheckHelper;
49
50
/**
51
 * do we have something that is a specific type of object?
52
 */
53
class IsObjectOfType implements Check, ListCheck
54
{
55
    // saves us having to implement inspectList() ourselves
56
    use ListCheckHelper;
57
58
    /**
59
     * what type of object are we looking for?
60
     * @var string
61
     */
62
    private $expectedType;
63
64
    /**
65
     * create a new check
66
     *
67
     * @param string $expectedType
68
     *        the class or interface that we want to check against
69
     */
70
    public function __construct($expectedType)
0 ignored issues
show
Unused Code introduced by
The parameter $expectedType is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72
        $this->expectedType = $type;
0 ignored issues
show
Bug introduced by
The variable $type does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
73
    }
74
75
    /**
76
     * do we have something that is a specific type of object?
77
     *
78
     * @param  mixed $fieldOrVar
79
     *         the item to be checked
80
     * @param  string $expectedType
81
     *         the class or interface that we want to check against
82
     * @return bool
83
     *         TRUE if the item is the requested type
84
     *         FALSE otherwise
85
     */
86
    public static function check($fieldOrVar, $expectedType)
87
    {
88
        // make sure we have an object
89
        if (!is_object($fieldOrVar)) {
90
            return false;
91
        }
92
93
        if ($fieldOrVar instanceof $expectedType) {
94
            return true;
95
        }
96
97
        return false;
98
    }
99
100
    /**
101
     * do we have something that is a specific type of object?
102
     *
103
     * @param  mixed $fieldOrVar
104
     *         the item to be checked
105
     * @return bool
106
     *         TRUE if the item is the requested type
107
     *         FALSE otherwise
108
     */
109
    public function inspect($fieldOrVar)
110
    {
111
        return static::check($fieldOrVar, $this->expectedType);
112
    }
113
114
    /**
115
     * is every entry in $list an object of a given type?
116
     *
117
     * @param  mixed $list
118
     *         the list of items to be checked
119
     * @param  string $expectedType
120
     *         the class or interface that we want to check against
121
     * @return bool
122
     *         TRUE if every item in $list is an object of a given type
123
     *         FALSE otherwise
124
     */
125
    public static function checkList($list, $expectedType)
126
    {
127
        $check = new static;
0 ignored issues
show
Bug introduced by
The call to IsObjectOfType::__construct() misses a required argument $expectedType.

This check looks for function calls that miss required arguments.

Loading history...
128
        return $check->inspectList($list, $expectedType);
0 ignored issues
show
Unused Code introduced by
The call to IsObjectOfType::inspectList() has too many arguments starting with $expectedType.

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.

Loading history...
129
    }
130
}
131