Completed
Push — master ( d56194...c8d3eb )
by Oleg
11:26
created

TextFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 29.82 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 3
dl 17
loc 57
ccs 20
cts 20
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 9 2
A __construct() 0 11 1
A serialize() 9 9 2
A unserialize() 8 8 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
namespace Malezha\Menu\Factory;
3
4
use Illuminate\Contracts\Container\Container;
5
use Malezha\Menu\Contracts\Attributes;
6
use Malezha\Menu\Element\Text;
7
use Opis\Closure\SerializableClosure;
8
9
/**
10
 * Class TextFactory
11
 * @package Malezha\Menu\Factory
12
 * 
13
 * @property string $text
14
 * @property Attributes $attributes
15
 * @property mixed $displayRule
16
 */
17
class TextFactory extends AbstractElementFactory
18
{
19
    /**
20
     * @param array $parameters
21
     * @return Text
22
     */
23 7
    public function build($parameters = [])
24
    {
25 7
        $text = $this->app->make(Text::class, $this->mergeParameters($parameters));
26 7
        if (array_key_exists('displayRule', $this->parameters)) {
27 7
            $text->setDisplayRule($this->parameters['displayRule']);
28
        }
29
30 7
        return $text;
31
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36 10
    public function __construct(Container $container)
37
    {
38 10
        parent::__construct($container);
39
        
40 10
        $this->parameters = [
41 10
            'text' => null,
42
            'displayRule' => true,
43 10
            'attributes' => $this->app->make(Attributes::class, ['attributes' => []]),
44 10
            'view' => $this->getElementConfig(Text::class)['view'],
45
        ];
46 10
    }
47
48
49
    /**
50
     * @inheritDoc
51
     */
52 1 View Code Duplication
    public function serialize()
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...
53
    {
54 1
        $params = $this->parameters;
55 1
        if ($params['displayRule'] instanceof \Closure) {
56
            $params['displayRule'] = new SerializableClosure($params['displayRule']);
57
        }
58
59 1
        return serialize($params);
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65 1 View Code Duplication
    public function unserialize($serialized)
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...
66
    {
67 1
        parent::unserialize($serialized);
68
69 1
        if ($this->parameters['displayRule'] instanceof SerializableClosure) {
70
            $this->parameters['displayRule'] = $this->parameters['displayRule']->getClosure();
71
        }
72
    }
73
}