Completed
Push — php7/base ( 153b4b )
by Albert
07:56 queued 05:57
created

LegacyToMigrate   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 89.16%

Importance

Changes 0
Metric Value
wmc 29
lcom 0
cbo 5
dl 0
loc 207
ccs 74
cts 83
cp 0.8916
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getStringOrNull() 0 4 1
A sumOfIntegers() 0 13 1
A arraysSum() 0 10 1
A getUserName() 0 4 2
A saySomething() 0 7 1
A divideEnters() 0 14 4
B searchInText() 0 24 1
A callingClosures() 0 10 1
A nothingToReturn() 0 13 2
A getComposedName() 0 6 1
A getAllComposedNames() 0 11 2
A tooMuchExceptionsMakeUglyCode() 0 22 3
A functionThatFails() 0 4 1
A anotherFunctionThatAlsoFails() 0 4 1
A getLastCharOfAString() 0 4 1
A printForEachValue() 0 12 2
A doSomethingTooHeavy() 0 4 1
A strcmpFromC() 0 15 3
1
<?php
2
3
namespace Kata\PHP7Kata;
4
5
use Kata\OtherExamples\Form;
6
use Kata\OtherExamples\Example;
7
use Kata\OtherExamples\NotAnExample;
8
9
final class LegacyToMigrate
10
{
11
    const ANIMALS = [
12
        'dog',
13
        'cat',
14
        'bird'
15
    ];
16
17 1
    public function getStringOrNull($param)
18
    {
19 1
        return $param;
20
    }
21
22 1
    public function sumOfIntegers(...$ints)
23
    {
24
        // let's review that all items are integers!
25 1
        $result = array_map(
26
            function ($value)
27
            {
28 1
                return (int) $value;
29 1
            },
30
            $ints
31
        );
32
33 1
        return array_sum($result);
34
    }
35
36 1
    public function arraysSum(...$arrays)
37
    {
38 1
        return array_map(
39
            function (array $array)
40
            {
41 1
                return ($array);
42 1
            },
43
            $arrays
44
        );
45
    }
46
47 1
    public function getUserName($user): string
48
    {
49 1
        return isset($user['username']) ? $user['username'] : 'nobody';
50
    }
51
52 1
    public function strcmpFromC($a, $b)
53
    {
54 1
        if ($a == $b)
55
        {
56 1
            return 0;
57
        }
58 1
        elseif ($a > $b)
59
        {
60 1
            return 1;
61
        }
62
        else
63
        {
64 1
            return -1;
65
        }
66
    }
67
68 1
    public function saySomething()
69
    {
70 1
        $example        = new Example();
71 1
        $not_an_example = new NotAnExample();
72
73 1
        return $example->greetings() . ' ' . $not_an_example->greetings() . '!';
74
    }
75
76 1
    public function divideEnters($a, $b)
77
    {
78 1
        if (0 === $b)
79
        {
80
            throw new DivisionByZeroError();
81
        }
82
83 1
        if ($a === PHP_INT_MIN && -1 === $b)
84
        {
85
            throw new ArithmeticError();
86
        }
87
88 1
        return round($a / $b, 0);
89
    }
90
91 1
    public function searchInText()
92
    {
93 1
        $credit_card = 'daniel:madurell:visa:999888777';
94
95 1
        $credit_card = preg_replace_callback(
96 1
            '/([a-z]{1})/',
97
            function ($m)
98
            {
99 1
                return strtoupper($m[1]);
100 1
            },
101
            $credit_card
102
        );
103
104 1
        $credit_card = preg_replace_callback(
105 1
            '/([0-9]{1})/',
106 1
            function ($m)
0 ignored issues
show
Unused Code introduced by
The parameter $m is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
            {
108 1
                return 'XXX';
109 1
            },
110
            $credit_card
111
        );
112
113 1
        return $credit_card;
114
    }
115
116 1
    public function callingClosures()
117
    {
118 1
        $form  = new Form(1);
119 1
        $form2 = new Form(2);
120
121 1
        $getX = $form->getX();
122 1
        $getX = $getX->bindTo($form2);
123
124 1
        return $getX();
125
    }
126
127 1
    public function nothingToReturn(&$left, &$right)
128
    {
129 1
        if ($left === $right)
130
        {
131
            return;
132
        }
133
134 1
        $tmp   = $left;
135 1
        $left  = $right;
136 1
        $right = $tmp;
137
138 1
        return;
139
    }
140
141 1
    public function getComposedName($data, $number_user)
142
    {
143 1
        list($id, $name) = $data[$number_user];
144
145 1
        return $id . '_' . $name;
146
    }
147
148 1
    public function getAllComposedNames($data)
149
    {
150 1
        $all_composed_names = '';
151
152 1
        foreach ($data as list($id, $name))
153
        {
154 1
            $all_composed_names .= $id . '_' . $name . '|';
155
        }
156
157 1
        return $all_composed_names;
158
    }
159
160 1
    public function tooMuchExceptionsMakeUglyCode()
161
    {
162
        try
163
        {
164 1
            $this->functionThatFails();
165
        }
166 1
        catch (FirstException $e)
167
        {
168 1
            return false;
169
        }
170
171
        try
172
        {
173
            $this->anotherFunctionThatAlsoFails();
174
        }
175
        catch (SecondException $e)
176
        {
177
            return false;
178
        }
179
180
        return true;
181
    }
182
183 1
    private function functionThatFails()
184
    {
185 1
        throw new FirstException;
186
    }
187
188
    private function anotherFunctionThatAlsoFails()
189
    {
190
        throw new SecondException;
191
    }
192
193 1
    public function getLastCharOfAString($a_string)
194
    {
195 1
        return substr($a_string, -1);
196
    }
197
198 1
    public function printForEachValue()
199
    {
200
        //Yield me!
201 1
        $gen = range(1, 1);
202
203 1
        foreach ($gen as $key => $value)
204
        {
205 1
            $this->doSomethingTooHeavy($key, $value);
0 ignored issues
show
Unused Code introduced by
The call to the method Kata\PHP7Kata\LegacyToMi...::doSomethingTooHeavy() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
206
        }
207
208 1
        return $value;
0 ignored issues
show
Bug introduced by
The variable $value seems to be defined by a foreach iteration on line 203. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
209
    }
210
211 1
    private function doSomethingTooHeavy($key, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
212
    {
213
        //...
214 1
    }
215
}
216