Palindrome::checkUsingReverse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Ianriizky\CodingInterview\Palindrome;
4
5
class Palindrome
6
{
7
    /**
8
     * Create a new instance class.
9
     *
10
     * @param  string  $value
11
     * @return void
12
     */
13 3
    public function __construct(
14
        protected string $value
15
    ) {
16 3
        $this->value = $this->sanitize($value);
17 3
    }
18
19
    /**
20
     * Create a new instance class in a static way.
21
     *
22
     * @param  string  $value
23
     * @return static
24
     */
25 3
    public static function make(string $value)
26
    {
27 3
        return new static($value);
28
    }
29
30
    /**
31
     * Determine whether the given value is a palindrome or not using reverse way.
32
     *
33
     * @return bool
34
     */
35 1
    public function checkUsingReverse(): bool
36
    {
37 1
        $value = $this->value;
38
39 1
        $newValue = '';
40
41 1
        for ($index = strlen($value) - 1; $index >= 0; $index--) {
42 1
            $newValue .= $value[$index];
43
        }
44
45 1
        return $value === $newValue;
46
    }
47
48
    /**
49
     * Determine whether the given value is a palindrome or not using loop way.
50
     *
51
     * @return bool
52
     */
53 1
    public function checkUsingLoop(): bool
54
    {
55 1
        $value = $this->value;
56
57 1
        for ($index = 0; $index < floor(strlen($value) / 2); $index++) {
58 1
            $lastCharacterIndex = strlen($value) - ($index + 1);
59
60 1
            $firstCharacter = $value[$index];
61 1
            $lastCharacter = $value[$lastCharacterIndex];
62
63 1
            if ($firstCharacter !== $lastCharacter) {
64 1
                return false;
65
            }
66
        }
67
68 1
        return true;
69
    }
70
71
    /**
72
     * Determine whether the given value is a palindrome or not using recursive way.
73
     *
74
     * @param  int  $index
75
     * @return bool
76
     */
77 1
    public function checkUsingRecursive(int $index = 0): bool
78
    {
79 1
        $value = $this->value;
80
81 1
        if ($index < floor(strlen($value) / 2)) {
82 1
            $lastCharacterIndex = strlen($value) - ($index + 1);
83
84 1
            $firstCharacter = $value[$index];
85 1
            $lastCharacter = $value[$lastCharacterIndex];
86
87 1
            if ($firstCharacter !== $lastCharacter) {
88 1
                return false;
89
            }
90
91 1
            return $this->checkUsingRecursive($index + 1);
92
        }
93
94 1
        return true;
95
    }
96
97
    /**
98
     * Sanitize the given value by running some specific task.
99
     *
100
     * @return string
101
     */
102 3
    protected function sanitize(string $value): string
103
    {
104 3
        $value = str_replace(' ', '', $value);
105 3
        $value = str_replace('-', '', $value);
106 3
        $value = preg_replace('/[^A-Za-z0-9\-]/', '', $value);
107 3
        $value = mb_strtolower($value, 'UTF-8');
108
109 3
        return $value;
110
    }
111
}
112