Completed
Push — php7/albert ( 153b4b...f2a408 )
by Albert
02:54 queued 01:02
created

LegacyToMigrate::functionThatFails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Kata\PHP7Kata;
4
5
use Behat\Testwork\Ordering\Orderer\Orderer;
6
use Kata\OtherExamples\{
7
    Example, Form, NotAnExample
8
};
9
10
final class LegacyToMigrate
11
{
12
    const ANIMALS = [
13
        'dog',
14
        'cat',
15
        'bird'
16
    ];
17
18 1
    public function getStringOrNull(?string $param):? string
19
    {
20 1
        return $param;
21
    }
22
23 1
    public function sumOfIntegers(int ...$ints)
24
    {
25 1
        return array_sum($ints);
26
    }
27
28 1
    public function arraysSum(array ...$arrays)
29
    {
30
        return array_map(function (array $array) {
31 1
            return ($array);
32 1
        }, $arrays);
33
    }
34
35 1
    public function getUserName($user): string
36
    {
37 1
        return $user['username'] ?? 'nobody';
38
    }
39
40 1
    public function strcmpFromC($a, $b)
41
    {
42 1
        return $a <=> $b;
43
    }
44
45 1
    public function saySomething()
46
    {
47 1
        $example        = new Example();
48 1
        $not_an_example = new NotAnExample();
49
50 1
        return $example->greetings() . ' ' . $not_an_example->greetings() . '!';
51
    }
52
53 1
    public function divideEnters(int $a, int $b)
54
    {
55 1
        return round($a / $b, 0);
56
    }
57
58 1
    public function searchInText()
59
    {
60 1
        $credit_card = 'daniel:madurell:visa:999888777';
61
62 1
        $credit_card = preg_replace_callback_array([
63
            '/([a-z]{1})/' => function ($m) {
64 1
                return strtoupper($m[1]);
65 1
            },
66
            '/([0-9]{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...
67 1
                return 'XXX';
68 1
            }
69
70
        ], $credit_card);
71
72 1
        return $credit_card;
73
    }
74
75 1
    public function callingClosures()
76
    {
77 1
        $form  = new Form(1);
78 1
        $form2 = new Form(2);
79
80 1
        $getX = $form->getX();
81
82 1
        return $getX->call($form2);
83
    }
84
85 1
    public function nothingToReturn(&$left, &$right): void
86
    {
87 1
        if ($left === $right) {
88
            return;
89
        }
90
91 1
        $tmp   = $left;
92 1
        $left  = $right;
93 1
        $right = $tmp;
94
95 1
        return;
96
    }
97
98 1
    public function getComposedName($data, $number_user) : string
99
    {
100 1
        [$id, $name] = $data[$number_user];
0 ignored issues
show
Bug introduced by
The variable $id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
101
102 1
        return $id . '_' . $name;
103
    }
104
105 1
    public function getAllComposedNames($data)
106
    {
107 1
        $all_composed_names = '';
108
109 1
        foreach ($data as [$id, $name]) {
110 1
            $all_composed_names .= $id . '_' . $name . '|';
0 ignored issues
show
Bug introduced by
The variable $id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
111
        }
112
113 1
        return $all_composed_names;
114
    }
115
116 1
    public function tooMuchExceptionsMakeUglyCode()
117
    {
118
        try {
119 1
            $this->functionThatFails();
120
            $this->anotherFunctionThatAlsoFails();
121 1
        } catch (SecondException | FirstException $e) {
122 1
            return false;
123
        }
124
125
        return true;
126
    }
127
128 1
    private function functionThatFails()
129
    {
130 1
        throw new FirstException;
131
    }
132
133
    private function anotherFunctionThatAlsoFails()
134
    {
135
        throw new SecondException;
136
    }
137
138 1
    public function getLastCharOfAString(string $a_string): string
139
    {
140 1
        return substr($a_string, -1);
141
    }
142
143
    public function printForEachValue()
144
    {
145
        $gen = function () { return yield 1; };
146
147 1
        foreach ($gen() as $key => $value) {
148 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...
149
        }
150
151 1
        return $value;
0 ignored issues
show
Bug introduced by
The variable $value seems to be defined by a foreach iteration on line 147. 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...
152
    }
153
154 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...
155
    {
156
        //...
157 1
    }
158
}
159