Completed
Pull Request — master (#146)
by Alexander
06:11
created

AbstractPackage   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 27.78 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 96.55%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 11
c 3
b 1
f 0
lcom 0
cbo 2
dl 25
loc 90
ccs 28
cts 29
cp 0.9655
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
doCheckSettings() 0 1 ?
A __set() 13 13 3
A __get() 12 12 3
A setSettings() 0 8 2
A normalizeKey() 0 6 1
A checkSettings() 0 6 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
 * Yandex PHP Library
4
 *
5
 * @copyright NIX Solutions Ltd.
6
 * @link https://github.com/nixsolutions/yandex-php-library
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Yandex\Common;
13
14
use Yandex\Common\Exception\InvalidSettingsException;
15
use Yandex\Common\Exception\RealizationException;
16
17
/**
18
 * Package
19
 *
20
 * @category Yandex
21
 * @package  Common
22
 *
23
 * @author   Anton Shevchuk
24
 * @created  07.08.13 10:12
25
 */
26
abstract class AbstractPackage
27
{
28
    /**
29
     * __set
30
     *
31
     * @param string $key
32
     * @param mixed $value
33
     * @throws Exception\RealizationException
34
     * @throws Exception\InvalidSettingsException
35
     * @return self
36
     */
37 5 View Code Duplication
    public function __set($key, $value)
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...
38
    {
39 5
        $method = 'set' . ucfirst($key);
40
41 5
        if (method_exists($this, $method)) {
42 3
            $this->$method($value);
43 5
        } elseif (property_exists($this, $key)) {
44 1
            throw new RealizationException("Property `$key` required realization setter method `$method`");
45
        } else {
46 1
            throw new InvalidSettingsException("Configuration option `$key`` is undefined");
47
        }
48 3
        return $this;
49
    }
50
51
    /**
52
     * __get
53
     *
54
     * @param string $key
55
     * @throws Exception\RealizationException
56
     * @throws Exception\InvalidSettingsException
57
     * @return self
58
     */
59 4 View Code Duplication
    public function __get($key)
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...
60
    {
61 4
        $method = 'get' . ucfirst($key);
62
63 4
        if (method_exists($this, $method)) {
64 3
            return $this->$method($key);
65 1
        } elseif (property_exists($this, $key)) {
66 1
            throw new RealizationException("Property `$key` required realization getter method `$method`");
67
        } else {
68
            throw new InvalidSettingsException("Configuration option '$key' is undefined");
69
        }
70
    }
71
72
    /**
73
     * @param array $options
74
     * @return void
75
     */
76 2
    public function setSettings(array $options)
77
    {
78
        // apply options
79 2
        foreach ($options as $key => $value) {
80 2
            $key = $this->normalizeKey($key);
81 2
            $this->$key = $value;
82 2
        }
83 2
    }
84
85
    /**
86
     * checkOptions
87
     *
88
     * @throws Exception\InvalidSettingsException
89
     * @return void
90
     */
91 8
    public function checkSettings()
92
    {
93 8
        if (!$this->doCheckSettings()) {
94 2
            throw new InvalidSettingsException("Invalid configuration options of '".get_class($this)."' package");
95
        }
96 6
    }
97
98
    /**
99
     * Check package configuration
100
     *
101
     * @return boolean
102
     */
103
    abstract protected function doCheckSettings();
104
105
    /**
106
     * @param string $key
107
     * @return string
108
     */
109 2
    private function normalizeKey($key)
110
    {
111 2
        $option = str_replace('_', ' ', strtolower($key));
112 2
        $option = str_replace(' ', '', ucwords($option));
113 2
        return $option;
114
    }
115
}
116