Completed
Branch dev (d5d70c)
by Raffael
11:00
created

DeltaAttributeDecorator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 20.93 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
dl 18
loc 86
rs 10
c 0
b 0
f 0
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A decorate() 0 8 2
A addDecorator() 0 6 1
A getAttributes() 0 10 1
B translateAttributes() 18 18 5

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
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\Filesystem;
13
14
class DeltaAttributeDecorator
15
{
16
    /**
17
     * Custom attributes.
18
     *
19
     * @var array
20
     */
21
    protected $custom = [];
22
23
    /**
24
     * Decorate attributes.
25
     *
26
     * @param array $node
27
     * @param array $attributes
28
     *
29
     * @return array
30
     */
31
    public function decorate($node, array $attributes = []): array
32
    {
33
        if (0 === count($attributes)) {
34
            return $this->translateAttributes($node, $this->getAttributes($node));
35
        }
36
37
        return $this->translateAttributes($node, array_intersect_key($this->getAttributes($node), array_flip($attributes)));
38
    }
39
40
    /**
41
     * Add decorator.
42
     *
43
     * @param string  $attribute
44
     * @param Closure $decorator
45
     *
46
     * @return AttributeDecorator
47
     */
48
    public function addDecorator(string $attribute, Closure $decorator): self
49
    {
50
        $this->custom[$attribute] = $decorator;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Get Attributes.
57
     *
58
     * @param NodeInterface
59
     *
60
     * @return array
61
     */
62
    protected function getAttributes(array $node): array
63
    {
64
        return [
65
            'id' => (string) $node['id'],
66
            'deleted' => $node['deleted'],
67
            'changed' => $node['changed']->toDateTime()->format('c'),
68
            'path' => $node['path'],
69
            'directory' => $node['directory'],
70
        ];
71
    }
72
73
    /**
74
     * Execute closures.
75
     *
76
     * @param array $node
77
     * @param array $attributes
78
     *
79
     * @return array
80
     */
81 View Code Duplication
    protected function translateAttributes(array $node, array $attributes): array
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...
82
    {
83
        foreach ($attributes as $key => &$value) {
84
            if ($value instanceof Closure) {
0 ignored issues
show
Bug introduced by
The class Balloon\Filesystem\Closure does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
85
                $result = $value->call($this, $node);
86
87
                if (null === $result) {
88
                    unset($attributes[$key]);
89
                } else {
90
                    $value = $result;
91
                }
92
            } elseif ($value === null) {
93
                unset($attributes[$key]);
94
            }
95
        }
96
97
        return $attributes;
98
    }
99
}
100