DelegatorWritableTrait   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 75
Duplicated Lines 26.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 20
loc 75
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isWritable() 0 11 4
A setWritable() 0 12 4
A setRegistryWritableFalse() 10 10 4
A setRegistryWritableTrue() 10 10 4

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
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Config
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Config\Traits;
16
17
use Phossa2\Shared\Delegator\DelegatorTrait;
18
use Phossa2\Config\Interfaces\WritableInterface;
19
20
/**
21
 * DelegatorWritableTrait
22
 *
23
 * Writable for delegator
24
 *
25
 * @package Phossa2\Config
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     WritableInterface
28
 * @version 2.0.8
29
 * @since   2.0.0 added
30
 * @since   2.0.7 changed to Delegator\DelegatorTrait
31
 */
32
trait DelegatorWritableTrait
33
{
34
    use WritableTrait, DelegatorTrait;
35
36
    /**
37
     * Override `isWritable()` in the WritableTrait.
38
     *
39
     * Delegator's writability is base on its registries
40
     *
41
     * {@inheritDoc}
42
     */
43
    public function isWritable()/*# : bool */
44
    {
45
        foreach ($this->lookup_pool as $reg) {
46
            if ($reg instanceof WritableInterface && $reg->isWritable()) {
47
                $this->clearLookupCache();
48
                $this->setWritable($reg);
49
                return true;
50
            }
51
        }
52
        return false;
53
    }
54
55
    /**
56
     * Override `setWritable()` in the WritableTrait
57
     *
58
     * {@inheritDoc}
59
     */
60
    public function setWritable($writable)/*# : bool */
61
    {
62
        if (!is_bool($writable)) {
63
            $this->writable = $writable;
64
        } elseif (false === $writable) {
65
            return $this->setRegistryWritableFalse();
66
        } elseif (!$this->isWritable()) {
67
            return $this->setRegistryWritableTrue();
68
        }
69
70
        return true;
71
    }
72
73
    /**
74
     * Set writable to FALSE in all registries
75
     *
76
     * @return bool
77
     * @access protected
78
     */
79 View Code Duplication
    protected function setRegistryWritableFalse()/*# : bool */
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...
80
    {
81
        foreach ($this->lookup_pool as $reg) {
82
            if ($reg instanceof WritableInterface &&
83
                !$reg->setWritable(false)) {
84
                return false;
85
            }
86
        }
87
        return true;
88
    }
89
90
    /**
91
     * Set writable to TRUE at first matching registry
92
     *
93
     * @return bool
94
     * @access protected
95
     */
96 View Code Duplication
    protected function setRegistryWritableTrue()/*# : bool */
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...
97
    {
98
        foreach ($this->lookup_pool as $reg) {
99
            if ($reg instanceof WritableInterface &&
100
                $reg->setWritable(true)) {
101
                return true;
102
            }
103
        }
104
        return false;
105
    }
106
}
107