FirstClass::encodeString()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 1
dl 13
loc 13
ccs 11
cts 11
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * File containing FirstClass
4
 *
5
 * @package conquerorsoft/my_first_library
6
 * @author Christian Varela <[email protected]>
7
 * @copyright (c) 2017, Conqueror Soft Inc (http://www.conquerorsoft.com)
8
 */
9
10
namespace conquerorsoft\my_first_library;
11
12
/**
13
 * This class provides functionality for encoding and decoding alphanumerical strings including spaces
14
 * Other characters will throw an exception
15
 *
16
 * @author Christian Varela <[email protected]>
17
 */
18
class FirstClass
19
{
20
    /**
21
     * Encodes a string by a simple shift of characters
22
     *
23
     * @param string $string
24
     * @return string
25
     * @throws \Exception
26
     */
27 18 View Code Duplication
    public function encodeString($string)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29 18
        $string=strtolower($string);
30 18
        $src="abcdefghijklmnopqrstuvwxyz0123456789 ";
31 18
        $dst="jklmnopqrstuvwxyz0123456789abcdefghi ";
32 18
        for ($i=0; $i<strlen($string); $i++) {
33 15
            $pos=strpos($src, $string[$i]);
34 15
            if ($pos===false) {
35 3
                throw new \Exception("Please provide only numbers and alphanumerical characters");
36
            }
37 12
            $string[$i]=$dst[$pos];
38 8
        }
39 15
        return $string;
40
    }
41
42
    /**
43
     * Decodes a string by a simple shift of characters
44
     *
45
     * @param string $string
46
     * @return string
47
     * @throws \Exception
48
     */
49 18 View Code Duplication
    public function decodeString($string)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51 18
        $string=strtolower($string);
52 18
        $src="jklmnopqrstuvwxyz0123456789abcdefghi ";
53 18
        $dst="abcdefghijklmnopqrstuvwxyz0123456789 ";
54 18
        for ($i=0; $i<strlen($string); $i++) {
55 15
            $pos=strpos($src, $string[$i]);
56 15
            if ($pos===false) {
57 3
                throw new \Exception("Please provide only numbers and alphanumerical characters");
58
            }
59 12
            $string[$i]=$dst[$pos];
60 8
        }
61 15
        return $string;
62
    }
63
}
64