Passed
Push — master ( 70fecb...e20386 )
by Craig
02:48
created

Word::reverse()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
namespace CustomD\WordFinder;
3
4
use RuntimeException;
5
use Illuminate\Support\Str;
6
7
class Word
8
{
9
10
    public const HORIZONTAL = 0;
11
    public const VERTICAL = 1;
12
    public const DIAGONAL_LEFT_TO_RIGHT = 2;
13
    public const DIAGONAL_RIGHT_TO_LEFT = 3;
14
15
    protected int $start = 0;
16
    protected int $end = -1;
17
    protected int $orientation = 0;
18
    protected ?string $label = null;
19
    protected bool $inversed = false;
20
21
    public function __construct($start, $end, int $orientation = 0, ?string $label = null, bool $inversed = false)
22
    {
23
        $this->setStart($start)
24
            ->setEnd($end)
25
            ->setOrientation($orientation)
26
            ->setLabel($label)
27
            ->setinversed($inversed);
28
    }
29
30
    public static function createRandom(?int $start, ?string $label = null): self
31
    {
32
        return new self($start, -1, rand(0, 3), $label, rand(0, 1) === 1);
33
    }
34
35
    public function setStart(int $start): self
36
    {
37
        $this->start = $start;
38
        return $this;
39
    }
40
41
    public function setEnd($end): self
42
    {
43
        $this->end = $end;
44
        return $this;
45
    }
46
47
    public function setOrientation(int $orientation): self
48
    {
49
        if (! in_array($orientation, [0,1,2,3])) {
50
            throw new RuntimeException("Orientation not valid");
51
        }
52
        $this->orientation = $orientation;
53
        return $this;
54
    }
55
56
    public function setLabel(?string $label): self
57
    {
58
        if ($label !== null) {
59
            $this->label = $this->getInversed() ? $this->reverse($label) : $label;
60
        }
61
        return $this;
62
    }
63
64
    public function setInversed(bool $inversed): self
65
    {
66
        $this->inversed = $inversed;
67
        return $this;
68
    }
69
70
    public function getStart()
71
    {
72
        return $this->start;
73
    }
74
75
    public function getEnd()
76
    {
77
        return $this->end;
78
    }
79
80
    public function getOrientation(): int
81
    {
82
        return $this->orientation;
83
    }
84
85
    public function getLabel(bool $forceNonInverced = false): ?string
86
    {
87
        if ($forceNonInverced && $this->getInversed()) {
88
            return $this->reverse($this->label);
0 ignored issues
show
Bug introduced by
It seems like $this->label can also be of type null; however, parameter $str of CustomD\WordFinder\Word::reverse() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
            return $this->reverse(/** @scrutinizer ignore-type */ $this->label);
Loading history...
89
        }
90
        return $this->label;
91
    }
92
93
    public function getInversed(): bool
94
    {
95
        return $this->inversed;
96
    }
97
98
    public function reverse(string $str): string
99
    {
100
        return $str;
101
        $r = '';
0 ignored issues
show
Unused Code introduced by
$r = '' is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
102
        for ($i = Str::length($str); $i>=0; $i--) {
103
            $r .= Str::substr($str, $i, 1);
104
        }
105
        return $r;
106
    }
107
}
108