Completed
Push — master ( 2093be...99e81f )
by Ben
07:44
created

LineKey   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 5 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
dl 3
loc 60
ccs 18
cts 20
cp 0.9
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A get() 0 4 1
A getPageKey() 0 4 1
A validateKey() 3 6 4
A fromString() 0 4 1
A getAsLabel() 0 9 1
A sanitizeKey() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Thinktomorrow\Squanto\Domain;
4
5
use Thinktomorrow\Squanto\Exceptions\InvalidLineKeyException;
6
7
class LineKey
8
{
9
    private $key;
10
11 69
    public function __construct($key)
12
    {
13 69
        $this->validateKey($key);
14
15 63
        $this->key = $this->sanitizeKey($key);
16 63
    }
17
18
    public static function fromString($key)
19
    {
20
        return new self($key);
21
    }
22
23 51
    public function get()
24
    {
25 51
        return $this->key;
26
    }
27
28
    /**
29
     * Get suggestion for a label based on the key
30
     *
31
     * @return string
32
     */
33 51
    public function getAsLabel()
34
    {
35
        // Remove first part since that part equals the page
36 51
        $key = substr($this->key, strpos($this->key, '.')+1);
37
38 51
        $label = str_replace('.', ' ', $key);
39
40 51
        return ucfirst($label);
41
    }
42
43
    /**
44
     * Get Page identifier.
45
     * This is the first segment of the key
46
     *
47
     * @return string
48
     */
49 57
    public function getPageKey()
50
    {
51 57
        return substr($this->key, 0, strpos($this->key, '.'));
52
    }
53
54 63
    private function sanitizeKey($key)
55
    {
56 63
        return strtolower($key);
57
    }
58
59 69
    private function validateKey($key)
60
    {
61 69 View Code Duplication
        if (!$key || !is_string($key) || false === strpos($key, '.')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
62 6
            throw new InvalidLineKeyException('Invalid LineKey format ['.$key.', type: '.gettype($key).'] given. Must be a string containing at least two dot separated segments. E.g. about.title');
63
        }
64 63
    }
65
66
}
67