Completed
Pull Request — master (#5)
by Carlos C
13:10
created

DequeCommonMethods::addLast()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php namespace GenericCollections\Traits;
2
3
use GenericCollections\Internal\DoubleLinkedList;
4
5
/**
6
 * This trait include all deque standard methods to queue, stack and deque
7
 *
8
 * @property DoubleLinkedList $storage
9
 *
10
 * @package GenericCollections\Traits
11
 */
12
trait DequeCommonMethods
13
{
14
    /**
15
     * Protected method to do the checks for add and offer methods
16
     *
17
     * @param $element
18
     * @return string The reason why this would produce an error
19
     */
20 216
    private function checkAddOffer($element)
21
    {
22
        // always throw an exception if element is null and container does not allow nulls
23 216
        if (! $this->optionAllowNullMembers() && is_null($element)) {
0 ignored issues
show
Bug introduced by
It seems like optionAllowNullMembers() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
24 24
            throw new \InvalidArgumentException(
25 24
                'The ' . $this->containerInternalName() . ' does not allow null elements'
0 ignored issues
show
Bug introduced by
It seems like containerInternalName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
26 24
            );
27
        }
28
        // always throw an exception if element is not the correct type
29 192 View Code Duplication
        if (! $this->checkElementType($element)) {
0 ignored issues
show
Bug introduced by
It seems like checkElementType() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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...
30 18
            throw new \InvalidArgumentException(
31
                'Invalid element type;'
32 18
                . ' the ' . $this->containerInternalName() . ' ' . get_class($this)
0 ignored issues
show
Bug introduced by
It seems like containerInternalName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
33 18
                . ' was expecting a ' . $this->getElementType() . ' type'
0 ignored issues
show
Bug introduced by
It seems like getElementType() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
34 18
            );
35
        }
36
        // return the error message, add will throw an exception, offer will return false
37 174
        if ($this->optionUniqueValues() && $this->contains($element)) {
0 ignored issues
show
Bug introduced by
It seems like optionUniqueValues() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
38 18
            return 'The ' . $this->containerInternalName() . ' does not allow duplicated elements';
0 ignored issues
show
Bug introduced by
It seems like containerInternalName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39
        }
40
        // no error found
41 174
        return '';
42
    }
43
44 36 View Code Duplication
    public function addFirst($element)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
45
    {
46 36
        if ('' !== $errorMessage = $this->checkAddOffer($element)) {
47 3
            throw new \InvalidArgumentException($errorMessage);
48
        }
49 24
        $this->storage->unshift($element);
50 24
    }
51
52 156 View Code Duplication
    public function addLast($element)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
53
    {
54 156
        if ('' !== $errorMessage = $this->checkAddOffer($element)) {
55 6
            throw new \InvalidArgumentException($errorMessage);
56
        }
57 141
        $this->storage->push($element);
58 141
    }
59
60 21 View Code Duplication
    public function offerFirst($element)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
61
    {
62 21
        if ('' !== $this->checkAddOffer($element)) {
63 3
            return false;
64
        }
65 12
        $this->storage->unshift($element);
66 12
        return true;
67
    }
68
69 39 View Code Duplication
    public function offerLast($element)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
70
    {
71 39
        if ('' !== $this->checkAddOffer($element)) {
72 6
            return false;
73
        }
74 21
        $this->storage->push($element);
75 21
        return true;
76
    }
77
78 24
    public function getFirst()
79
    {
80 24
        if ($this->isEmpty()) {
0 ignored issues
show
Bug introduced by
It seems like isEmpty() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
81 6
            throw new \LogicException('Can not get an element from an empty ' . $this->containerInternalName());
0 ignored issues
show
Bug introduced by
It seems like containerInternalName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
82
        }
83 18
        return $this->storage->bottom();
84
    }
85
86 6
    public function peekFirst()
87
    {
88 6
        if ($this->isEmpty()) {
0 ignored issues
show
Bug introduced by
It seems like isEmpty() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
89 6
            return null;
90
        }
91 6
        return $this->storage->bottom();
92
    }
93
94 18
    public function removeFirst()
95
    {
96 18
        if ($this->isEmpty()) {
0 ignored issues
show
Bug introduced by
It seems like isEmpty() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
97 6
            throw new \LogicException('Can not remove an element from an empty ' . $this->containerInternalName());
0 ignored issues
show
Bug introduced by
It seems like containerInternalName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
98
        }
99 12
        return $this->storage->shift();
100
    }
101
102 12
    public function pollFirst()
103
    {
104 12
        if ($this->isEmpty()) {
0 ignored issues
show
Bug introduced by
It seems like isEmpty() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
105 6
            return null;
106
        }
107 12
        return $this->storage->shift();
108
    }
109
110 27
    public function contains($element)
111
    {
112 27
        return $this->storage->contains($element);
113
    }
114
115 243
    protected function createStorageObject()
116
    {
117 243
        $this->storage = new DoubleLinkedList();
118 243
        if (! $this->optionComparisonIsIdentical()) {
0 ignored issues
show
Bug introduced by
It seems like optionComparisonIsIdentical() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
119 6
            $this->storage->strictComparisonOff();
120 6
        }
121 243
    }
122
}
123