InterfaceDefinition::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 4
nc 8
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * \AppserverIo\Doppelgaenger\Entities\Definitions\InterfaceDefinition
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Bernhard Wick <[email protected]>
15
 * @copyright 2015 TechDivision GmbH - <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/doppelgaenger
18
 * @link      http://www.appserver.io/
19
 */
20
21
namespace AppserverIo\Doppelgaenger\Entities\Definitions;
22
23
use AppserverIo\Doppelgaenger\Entities\Lists\AssertionList;
24
use AppserverIo\Doppelgaenger\Entities\Lists\FunctionDefinitionList;
25
use AppserverIo\Doppelgaenger\Entities\Lists\TypedListList;
26
27
/**
28
 * This class acts as a DTO-like (we are not immutable due to protected visibility)
29
 * entity for describing interface definitions
30
 *
31
 * @author    Bernhard Wick <[email protected]>
32
 * @copyright 2015 TechDivision GmbH - <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/appserver-io/doppelgaenger
35
 * @link      http://www.appserver.io/
36
 */
37
class InterfaceDefinition extends AbstractStructureDefinition
38
{
39
40
    /**
41
     * @const string TYPE The structure type
42
     */
43
    const TYPE = 'interface';
44
45
    /**
46
     * The parent interfaces (if any)
47
     *
48
     * @var string $extends
49
     */
50
    protected $extends;
51
52
    /**
53
     * Possible constants the interface defines
54
     *
55
     * @var array $constants
56
     */
57
    protected $constants;
58
59
    /**
60
     * Default constructor
61
     *
62
     * TODO The constructor does not use all members
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
63
     *
64
     * @param string                      $docBlock            DocBlock header of the interface
65
     * @param string                      $name                $name Interface name
66
     * @param string                      $namespace           The namespace the definition resides in
67
     * @param array                       $extends             The parent interfaces (if any)
68
     * @param array                       $constants           Possible constants the interface defines
69
     * @param AssertionList|null          $invariantConditions Invariant conditions
70
     * @param AssertionList|null          $ancestralInvariants Ancestral invariants
71
     * @param FunctionDefinitionList|null $functionDefinitions List of functions defined within the interface
72
     */
73
    public function __construct(
74
        $docBlock = '',
75
        $name = '',
76
        $namespace = '',
77
        $extends = array(),
78
        $constants = array(),
79
        $invariantConditions = null,
80
        $ancestralInvariants = null,
81
        $functionDefinitions = null
82
    ) {
83
        $this->docBlock = $docBlock;
84
        $this->name = $name;
85
        $this->namespace = $namespace;
86
        $this->extends = $extends;
0 ignored issues
show
Documentation Bug introduced by
It seems like $extends of type array is incompatible with the declared type string of property $extends.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
87
        $this->constants = $constants;
88
        $this->invariantConditions = is_null($invariantConditions) ? new AssertionList() : $invariantConditions;
89
        $this->ancestralInvariants = is_null($ancestralInvariants) ? new TypedListList() : $ancestralInvariants;
0 ignored issues
show
Documentation Bug introduced by
It seems like is_null($ancestralInvari... : $ancestralInvariants can also be of type object<AppserverIo\Doppe...es\Lists\AssertionList>. However, the property $ancestralInvariants is declared as type object<AppserverIo\Doppe...es\Lists\TypedListList>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
90
        $this->functionDefinitions = is_null(
91
            $functionDefinitions
92
        ) ? new FunctionDefinitionList() : $functionDefinitions;
93
    }
94
95
    /**
96
     * Getter method for attribute $constants
97
     *
98
     * @return array
99
     */
100
    public function getConstants()
101
    {
102
        return $this->constants;
103
    }
104
105
    /**
106
     * Will return a list of all dependencies eg. parent class, interfaces and traits.
107
     *
108
     * @return array
109
     */
110
    public function getDependencies()
111
    {
112
        // Get our interfaces
113
        $result = $this->extends;
114
115
        // We got an error that this is nor array, weird but build up a final frontier here
116
        if (!is_array($result)) {
117
            $result = array($result);
118
        }
119
120
        return $result;
121
    }
122
123
    /**
124
     * Getter method for attribute $extends
125
     *
126
     * @return string
127
     */
128
    public function getExtends()
129
    {
130
        return $this->extends;
131
    }
132
133
    /**
134
     * Setter method for attribute $ancestralInvariants
135
     *
136
     * @param \AppserverIo\Doppelgaenger\Entities\Lists\AssertionList $ancestralInvariants Inherited invariant assertions
137
     *
138
     * @return null
139
     */
140
    public function setAncestralInvariants(AssertionList $ancestralInvariants)
141
    {
142
        $this->ancestralInvariants = $ancestralInvariants;
0 ignored issues
show
Documentation Bug introduced by
It seems like $ancestralInvariants of type object<AppserverIo\Doppe...es\Lists\AssertionList> is incompatible with the declared type object<AppserverIo\Doppe...es\Lists\TypedListList> of property $ancestralInvariants.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
143
    }
144
145
    /**
146
     * Setter method for the $constants property
147
     *
148
     * @param array $constants Constants the class defines
149
     *
150
     * @return null
151
     */
152
    public function setConstants($constants)
153
    {
154
        $this->constants = $constants;
155
    }
156
157
    /**
158
     * Setter method for the $extends property
159
     *
160
     * @param string $extends Potential parent class
161
     *
162
     * @return null
163
     */
164
    public function setExtends($extends)
165
    {
166
        $this->extends = $extends;
167
    }
168
169
    /**
170
     * Setter method for attribute $invariantConditions
171
     *
172
     * @param \AppserverIo\Doppelgaenger\Entities\Lists\AssertionList $invariantConditions List of invariant assertions
173
     *
174
     * @return null
175
     */
176
    public function setInvariantConditions(AssertionList $invariantConditions)
177
    {
178
        $this->invariantConditions = $invariantConditions;
179
    }
180
}
181