Tuple::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 16
Ratio 76.19 %

Code Coverage

Tests 9
CRAP Score 3.4098

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 1
dl 16
loc 21
ccs 9
cts 14
cp 0.6429
crap 3.4098
rs 9.3142
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * This file is part of the Tuple package.
5
 * For the full copyright information please view the LICENCE file that was
6
 * distributed with this package.
7
 *
8
 * @copyright Simon Deeley 2017
9
 */
10
11
namespace simondeeley;
12
13
use SplFixedArray;
14
use InvalidArgumentException;
15
use LengthException;
16
use simondeeley\Type\Type;
17
use simondeeley\Type\TupleType;
18
use simondeeley\Type\TypeEquality;
19
use simondeeley\ImmutableArrayTypeObject;
20
use simondeeley\Helpers\TypeEqualityHelperMethods;
21
22
/**
23
 * Tuple base object
24
 *
25
 * @author Simon Deeley <[email protected]>
26
 * @abstract
27
 * @uses ImmutableArrayTypeObject
28
 */
29
abstract class Tuple extends ImmutableArrayTypeObject implements TupleType, TypeEquality
30
{
31
    use TypeEqualityHelperMethods;
32
33
    const MAX_LENGTH = PHP_INT_MAX;
34
    const MIN_LENGTH = 0;
35
36
    /**
37
     * @var SplFixedArray $data
38
     */
39
    protected $data;
40
41
    /**
42
     * Create a new tuple object
43
     *
44
     * Instantiates a new tuple object of fixed length with the provided data
45
     * set. Once the object is created, it's contents cannot be mutated.
46
     *
47
     * @param mixed ...$items - Variadic number of arguments
48
     * @return void
49
     * @throws LengthException - Thrown if number of arguments passed exceeds
50
     *                           the maximum or is less than the minimum allowed
51
     *                           values.
52
     */
53 24
    final public function __construct(...$items)
54
    {
55 24 View Code Duplication
        if (count($items) < static::MIN_LENGTH) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56 6
            throw new LengthException(sprintf(
57 6
                '%s expects a minimum of %d arguments but only got %d',
58 6
                $this::getType(),
59 6
                static::MIN_LENGTH,
60 6
                count($items)
61
            ));
62
        }
63
64 18 View Code Duplication
        if (count($items) > static::MAX_LENGTH) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
            throw new LengthException(sprintf(
66
                '%s expects a maximum of %d arguments but instead got %d',
67
                $this::getType(),
68
                static::MAX_LENGTH,
69
                count($items)
70
            ));
71
        }
72
73 18
        $this->data = SplFixedArray::fromArray($items);
74 18
    }
75
76
    /**
77
     * Check two Tuples are equal
78
     *
79
     * Compares the internal data of the two tuple objects ($tuple & $this). It
80
     * runs each through a comparitor which serializes each value, if one is
81
     * not equal to the other then the result will be considered false.
82
     *
83
     * @param Type $tuple - The tuple to compare
84
     * @param int $flags - optional bitwise flags to change method behaviour
85
     * @throws InvalidArgumentException - thrown if $tuple is not an instance of
86
     *                                    TupleType
87
     * @return bool - Returns true if $tuple is equal to $this
88
     */
89 6
    final public function equals(Type $tuple, int $flags = TypeEquality::IGNORE_OBJECT_IDENTITY): bool
90
    {
91 6
        if (false === $tuple instanceof TupleType)
92
        {
93
            throw new InvalidArgumentException(sprintf(
94
                'Cannot compare %s with %s as they are not of the same type',
95
                get_class($this),
96
                get_class($tuple)
97
            ));
98
        }
99
100 6
        if (0 === strcmp(
101 6
                md5(serialize($this->data->toArray())),
102 6
                md5(serialize($tuple->data->toArray())))
0 ignored issues
show
Bug introduced by
Accessing data on the interface simondeeley\Type\TupleType suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
103 6
            && $this->isSameTypeAs($tuple, $flags)
104 6
            && $this->isSameObjectAs($tuple, $flags)
105
        ) {
106 6
            return true;
107
        }
108
109
        return false;
110
    }
111
112
    /**
113
     * Check that a property exists
114
     *
115
     * @see http://php.net/manual/en/arrayaccess.offsetexists.php
116
     *
117
     * @param mixed $offset - Name of the property to check
118
     * @return bool - Returns true if property exists
119
     * @throws InvalidArgumentException - Thrown if $property is not an integer
120
     */
121 18
    final public function offsetExists($offset): bool
122
    {
123 18
        if (false === is_int($offset)) {
124 6
            throw new InvalidArgumentException(sprintf(
125 6
                'Offset must be of type integer, "%s" passed',
126 6
                 gettype($offset)
127
            ));
128
        }
129
130 12
        return isset($this->data[$offset]);
131
    }
132
133
    /**
134
     * ArrayAccess get offset
135
     *
136
     * @param mixed $offset
137
     * @return mixed
138
     */
139 18
    final public function offsetGet($offset)
140
    {
141 18
        return $this->offsetExists($offset) ? $this->data[$offset] : null;
142
    }
143
144
    /**
145
     * Implement a basic getType
146
     *
147
     * @return string
148
     */
149
    public static function getType(): string
150
    {
151
        return 'TUPLE';
152
    }
153
}
154