Passed
Push — master ( aa6768...d07215 )
by Simon
13:02 queued 45s
created

Set::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * This file is part of the Set 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 simondeeley\Type\Type;
15
use simondeeley\Type\SetType;
16
use simondeeley\Type\TypeEquality;
17
use simondeeley\ImmutableArrayTypeObject;
18
use simondeeley\Helpers\TypeEqualityHelperMethods;
19
20
/**
21
 * Set type object
22
 *
23
 * Implements a mathematical set in PHP. A mathematical set is an unordered list
24
 * of items where two lists are considered equal if both sets contain the same
25
 * items but not necessarily in the same order or in the same quantity. So, for
26
 * example:
27
 *      Set(1, 2, 3) === Set(3, 1, 2);
28
 *      Set(1, 2, 3) === Set(1, 2, 2, 3);
29
 * but
30
 *      Set(1, 2) !== Set(1, 2, 3);
31
 *
32
 * @author Simon Deeley <[email protected]>
33
 */
34
abstract class Set extends ImmutableArrayTypeObject implements SetType, TypeEquality
35
{
36
    use TypeEqualityHelperMethods;
37
38
    /**
39
     * @var SplFixedArray
40
     */
41
    protected $data;
42
43
    /**
44
     * Construct a new Set
45
     *
46
     * @final
47
     * @param mixed ...$items - accepts any number of items
48
     * @return void
49
     */
50
    final public function __construct(...$items)
51
    {
52
        $this->data = SplFixedArray::fromArray($items);
53
    }
54
55
    /**
56
     * Compare equality of two Sets
57
     *
58
     * @final
59
     * @param Type $set - The set to compare with
60
     * @param int $flags - Optional flags
61
     * @return bool
62
     * @throws InvalidArgumentException - thrown if $type is not of an instance
63
     *                                    of {@link Set}
64
     */
65
    final public function equals(Type $set, int $flags = TypeEquality::IGNORE_OBJECT_IDENTITY): bool
66
    {
67
        if (false === $set instanceof SetType) {
68
            throw new InvalidArgumentException(sprintf(
0 ignored issues
show
Bug introduced by
The type simondeeley\InvalidArgumentException was not found. Did you mean InvalidArgumentException? If so, make sure to prefix the type with \.
Loading history...
69
                'Cannot compare %s with %s as they are not of the same type',
70
                get_class($this),
71
                get_class($tuple)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tuple seems to be never defined.
Loading history...
72
            ));
73
        }
74
75
        // Loop through items in $this, if any do not exist in $set then the
76
        // two Sets are not equal.
77 View Code Duplication
        foreach($this->data as $item) {
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...
78
            if (false === in_array($item, $set->data->toArray(), true)) {
1 ignored issue
show
Bug introduced by
Accessing data on the interface simondeeley\Type\SetType suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
79
                return false;
80
            }
81
        }
82
83
        // Loop through items in $set. If any do not exist in $this then they
84
        // they are not equal.
85 View Code Duplication
        foreach ($set->data as $item) {
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...
86
            if (false === in_array($item, $this->data->toArray(), true)) {
87
                return false;
88
            }
89
        }
90
91
        return $this->isSameTypeAs($set, $flags)
92
            && $this->isSameObjectAs($set, $flags);
93
    }
94
95
    /**
96
     * Return the type of the object
97
     *
98
     * @return string
99
     */
100
    public static function getType(): string
101
    {
102
        return 'SET';
103
    }
104
}
105