Completed
Push — php7/xavi ( 89a0e4 )
by
unknown
03:42 queued 01:43
created

LegacyToMigrate::divideEnters()   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 2
crap 1
1
<?php
2
3
namespace Kata\PHP7Kata;
4
5
use Kata\OtherExamples\{Form,Example,NotAnExample};
6
7
final class LegacyToMigrate
8
{
9
    public const ANIMALS = [
10
        'dog',
11
        'cat',
12
        'bird'
13
    ];
14
15 1
    public function getStringOrNull(?string $param): ?string
16
    {
17 1
        return $param;
18
    }
19
20 1
    public function sumOfIntegers(int ...$ints)
21
    {
22 1
        return array_sum($ints);
23
    }
24
25 1
    public function arraysSum(array ...$arrays)
26
    {
27 1
        return $arrays;
28
    }
29
30 1
    public function getUserName($user): string
31
    {
32 1
        return $user['username'] ?? 'nobody';
33
    }
34
35 1
    public function strcmpFromC($a, $b)
36
    {
37 1
        return $a <=> $b;
38
    }
39
40 1
    public function saySomething()
41
    {
42 1
        $example        = new Example();
43 1
        $not_an_example = new NotAnExample();
44
45 1
        return $example->greetings() . ' ' . $not_an_example->greetings() . '!';
46
    }
47
48 1
    public function divideEnters($a, $b)
49
    {
50 1
        return intdiv($a, $b);
51
    }
52
53 1
    public function searchInText()
54
    {
55 1
        $credit_card = 'daniel:madurell:visa:999888777';
56
57 1
        $credit_card = preg_replace_callback_array([
58
            '/([a-z]{1})/' => function ($matches) {
59 1
                return strtoupper($matches[1]);
60 1
            },
61
            '/([0-9]{1})/' => function ($matches) {
0 ignored issues
show
Unused Code introduced by
The parameter $matches 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...
62 1
                return 'XXX';
63 1
            }
64
        ], $credit_card);
65
66 1
        return $credit_card;
67
    }
68
69 1
    public function callingClosures()
70
    {
71 1
        $form  = new Form(1);
72 1
        $form2 = new Form(2);
73
74 1
        $getX = $form->getX();
75 1
        return $getX->call($form2);
76
    }
77
78 1
    public function nothingToReturn(&$left, &$right): void
79
    {
80 1
        if ($left === $right)
81
        {
82
            return;
83
        }
84
85 1
        $tmp   = $left;
86 1
        $left  = $right;
87 1
        $right = $tmp;
88
89 1
        return;
90
    }
91
92 1
    public function getComposedName($data, $number_user)
93
    {
94 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...
95
96 1
        return $id . '_' . $name;
97
    }
98
99 1
    public function getAllComposedNames($data)
100
    {
101 1
        $all_composed_names = '';
102
103 1
        foreach ($data as [$id, $name])
104
        {
105 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...
106
        }
107
108 1
        return $all_composed_names;
109
    }
110
111 1
    public function tooMuchExceptionsMakeUglyCode()
112
    {
113
        try
114
        {
115 1
            $this->functionThatFails();
116
            $this->anotherFunctionThatAlsoFails();
117
        }
118 1
        catch (FirstException | SecondException $e)
119
        {
120 1
            return false;
121
        }
122
123
        return true;
124
    }
125
126 1
    private function functionThatFails()
127
    {
128 1
        throw new FirstException;
129
    }
130
131
    private function anotherFunctionThatAlsoFails()
132
    {
133
        throw new SecondException;
134
    }
135
136 1
    public function getLastCharOfAString($a_string)
137
    {
138 1
        return $a_string[-1];
139
    }
140
141
    public function printForEachValue()
142
    {
143 1
        $gen = function ()
144
        {
145 1
            yield 1;
146 1
        };
147
148 1
        foreach ($gen() as $key => $value)
149
        {
150 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...
151
        }
152
153 1
        return $value;
0 ignored issues
show
Bug introduced by
The variable $value seems to be defined by a foreach iteration on line 148. 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...
154
    }
155
156 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...
157
    {
158
        //...
159 1
    }
160
}
161