Stack   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 124
Duplicated Lines 18.55 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 23
loc 124
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A peek() 0 3 2
A isFull() 7 7 3
A count() 0 3 1
A size() 0 3 1
A empty() 0 3 1
A push() 8 17 3
A pop() 0 11 2
A __construct() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * DataStructures for PHP
4
 *
5
 * @link      https://github.com/SiroDiaz/DataStructures
6
 * @copyright Copyright (c) 2017 Siro Díaz Palazón
7
 * @license   https://github.com/SiroDiaz/DataStructures/blob/master/README.md (MIT License)
8
 */
9
namespace DataStructures\Lists;
10
11
use DataStructures\Lists\Nodes\SinglyLinkedListNode as Node;
12
use DataStructures\Exceptions\FullException;
13
use InvalidArgumentException;
14
use Countable;
15
16
/**
17
 * Stack
18
 *
19
 * Stack (LIFO) is a singly linked list that inserts and removes at the
20
 * beginnig of list. Insert and remove are O(1), size and empty are also O(1).
21
 *
22
 * @author Siro Diaz Palazon <[email protected]>
23
 */
24
class Stack implements Countable {
25
    /**
26
     * @var $head represents the stack head.
27
     */
28
    private $head;
29
    /**
30
     * @var $size contains the stack size. Increments each time
31
     *  that is inserted a new item.
32
     */
33
    private $size;
34
    /**
35
     * @var $maxSize if 0 it means that stack is unlimited, else it indicates
36
     *  the maximum size of the stack.
37
     */
38
    private $maxSize;
39
40
    /**
41
     * Initializes the stack.
42
     *
43
     * @param int $maxSize 0 by default. If greater than 0 is limited.
44
     * @throws \InvalidArgumentException if size is lower than 0.
45
     */
46 View Code Duplication
    public function __construct($maxSize = 0) {
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...
47
        if($maxSize < 0) {
48
            throw new InvalidArgumentException();
49
        }
50
        $this->head = null;
51
        $this->maxSize = $maxSize;
52
        $this->size = 0;
53
    }
54
55
    /**
56
     * Returns the stack size.
57
     *
58
     * @return int the length
59
     */
60
    public function size() : int {
61
        return $this->size;
62
    }
63
    
64
65
    /**
66
     * Checks if the stack is empty.
67
     *
68
     * @return boolean true if is empty, else false.
69
     */
70
    public function empty() : bool {
71
        return $this->size === 0;
72
    }
73
74
    /**
75
     * Adds at the end of the stack new node containing
76
     * the data to be stored.
77
     *
78
     * @param mixed $data The data
79
     * @throws DataStructures\Exceptions\FullException if the queue is full.
80
     */
81
    public function push($data) {
82
        if($this->isFull()) {
83
            throw new FullException();
84
        }
85
86
        $newNode = new Node($data);
87 View Code Duplication
        if($this->head === null) {
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...
88
            $this->head = &$newNode;
89
            $newNode->next = null;
90
        } else {
91
            $temp = $this->head;
92
            $this->head = &$newNode;
93
            $newNode->next = &$temp;
94
        }
95
96
        $this->size++;
97
    }
98
99
    /**
100
     * Removes and returns the last node in the stack.
101
     *
102
     * @return mixed data in node.
103
     */
104
    public function pop() {
105
        if($this->head === null) {
106
            return null;
107
        }
108
        
109
        $node = $this->head;
110
        $this->head = $this->head->next;
111
        $this->size--;
112
113
        return $node->data;
114
    }
115
116
    /**
117
     * Gets the element at the front of the stack without removing it.
118
     *
119
     * @return mixed
120
     */
121
    public function peek() {
122
        return ($this->head === null) ? null : $this->head->data;
123
    }
124
125
    /**
126
     * Returns true if is full the stack and false if there is
127
     * space available.
128
     *
129
     * @return bool 
130
     */
131 View Code Duplication
    public function isFull() {
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...
132
        if($this->maxSize === 0) {
133
            return false;
134
        }
135
        
136
        return $this->size > 0 && $this->size >= $this->maxSize;
137
    }
138
139
    /**
140
     * Binds to count() method. This is equal to make $this->stack->size().
141
     *
142
     * @return integer the stack size. 0 if it is empty.
143
     */
144
    public function count() {
145
        return $this->size;
146
    }
147
}