Completed
Push — master ( 8c886d...91409e )
by Ben
03:10
created

PageKey   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 68
Duplicated Lines 4.41 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 3
loc 68
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fromString() 0 4 1
A fromLineKeyString() 0 5 2
A get() 0 4 1
A getAsLabel() 0 4 1
A isExcludedSource() 0 6 1
A sanitizeKey() 0 4 1
A validateKey() 3 6 4
A getExcludedSources() 0 9 2

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\InvalidPageKeyException;
6
7
class PageKey
8
{
9
    private static $excludedSources;
10
    private $key;
11
12 14
    public function __construct($key)
13
    {
14 14
        $this->validateKey($key);
15
16 12
        $this->key = $this->sanitizeKey($key);
17 12
    }
18
19 1
    public static function fromString($key)
20
    {
21 1
        return new self($key);
22
    }
23
24 9
    public static function fromLineKeyString($key)
25
    {
26 9
        $key = false !== strpos($key,'.') ? substr($key, 0, strpos($key, '.')) : $key;
27 9
        return new self($key);
28
    }
29
30 1
    public function get()
31
    {
32 1
        return $this->key;
33
    }
34
35
    /**
36
     * Get suggestion for a label based on the key
37
     *
38
     * @return string
39
     */
40 1
    public function getAsLabel()
41
    {
42 1
        return ucfirst($this->key);
43
    }
44
45 10
    public function isExcludedSource()
46
    {
47 10
        $excluded = $this->getExcludedSources();
48
49 10
        return in_array($this->key,$excluded);
50
    }
51
52 12
    private function sanitizeKey($key)
53
    {
54 12
        return strtolower($key);
55
    }
56
57 14
    private function validateKey($key)
58
    {
59 14 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...
60 2
            throw new InvalidPageKeyException('Invalid PageKey format ['.$key.', type: '.gettype($key).'] given. Must be a string without dot separated segments. E.g. about and not about.title');
61
        }
62 12
    }
63
64 10
    private function getExcludedSources()
65
    {
66 10
        if(!self::$excludedSources)
67
        {
68 1
            self::$excludedSources = config('squanto.excluded_files',[]);
69
        }
70
71 10
        return self::$excludedSources;
72
    }
73
74
}
75