Completed
Push — master ( 1ee05e...99144a )
by Gaetano
05:13
created

ContentVersionManager   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 149
Duplicated Lines 35.57 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 6
dl 53
loc 149
c 0
b 0
f 0
ccs 0
cts 61
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 22 22 1
A load() 0 8 1
A delete() 19 19 3
C matchVersions() 12 38 13
A getReferencesValues() 0 22 4
A versionStatusToHash() 0 11 3

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 Kaliop\eZMigrationBundle\Core\Executor;
4
5
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
6
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
7
use Kaliop\eZMigrationBundle\Core\Matcher\ContentVersionMatcher;
8
9
/**
10
 * Handles content-version migrations.
11
 * @todo disallow calling (throw): create, update, generateMigration
12
 */
13
class ContentVersionManager extends ContentManager
14
{
15
    protected $supportedStepTypes = array('content_version');
16
    protected $supportedActions = array('delete', 'load');
17
18
    protected $versionMatcher;
19
20 View Code Duplication
    public function __construct(
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...
21
        ContentMatcher $contentMatcher,
22
        SectionMatcher $sectionMatcher,
23
        UserMatcher $userMatcher,
24
        ObjectStateMatcher $objectStateMatcher,
25
        ObjectStateGroupMatcher $objectStateGroupMatcher,
26
        FieldHandlerManager $fieldHandlerManager,
27
        LocationManager $locationManager,
28
        SortConverter $sortConverter,
29
        ContentVersionMatcher $versionMatcher
30
    )
31
    {
32
        $this->contentMatcher = $contentMatcher;
33
        $this->sectionMatcher = $sectionMatcher;
34
        $this->userMatcher = $userMatcher;
35
        $this->objectStateMatcher = $objectStateMatcher;
36
        $this->objectStateGroupMatcher = $objectStateGroupMatcher;
37
        $this->fieldHandlerManager = $fieldHandlerManager;
38
        $this->locationManager = $locationManager;
39
        $this->sortConverter = $sortConverter;
40
        $this->versionMatcher = $versionMatcher;
41
    }
42
43
    protected function load($step)
44
    {
45
        $versionCollection = $this->matchVersions('load', $step);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->matchVersions('load', $step); of type array|ArrayObject adds the type array to the return on line 49 which is incompatible with the return type of the parent method Kaliop\eZMigrationBundle...or\ContentManager::load of type Kaliop\eZMigrationBundle...ction\ContentCollection.
Loading history...
46
47
        $this->setReferences($versionCollection, $step);
0 ignored issues
show
Documentation introduced by
$versionCollection is of type array|object<ArrayObject>, but the function expects a object<Object>|object<Ka...ion\AbstractCollection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
49
        return $versionCollection;
50
    }
51
52
    /**
53
     * Handles the content delete migration action type
54
     */
55 View Code Duplication
    protected function delete($step)
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...
56
    {
57
        $versionCollection = $this->matchVersions('delete', $step);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->matchVersions('delete', $step); of type array|ArrayObject adds the type array to the return on line 72 which is incompatible with the return type of the parent method Kaliop\eZMigrationBundle...\ContentManager::delete of type Kaliop\eZMigrationBundle...ction\ContentCollection.
Loading history...
58
59
        $this->setReferences($versionCollection, $step);
0 ignored issues
show
Documentation introduced by
$versionCollection is of type array|object<ArrayObject>, but the function expects a object<Object>|object<Ka...ion\AbstractCollection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
61
        $contentService = $this->repository->getContentService();
62
63
        foreach ($versionCollection as $versionInfo) {
64
            try {
65
                $contentService->deleteVersion($versionInfo);
66
            } catch (NotFoundException $e) {
67
                // Someone else (or even us, by virtue of location tree?) removed the content which we found just a
68
                // second ago. We can safely ignore this
69
            }
70
        }
71
72
        return $versionCollection;
73
    }
74
75
    /**
76
     * @param string $action
77
     * @return ContentCollection
78
     * @throws \Exception
79
     */
80
    protected function matchVersions($action, $step)
81
    {
82 View Code Duplication
        if (!isset($step->dsl['object_id']) && !isset($step->dsl['remote_id']) && !isset($step->dsl['match'])) {
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...
83
            throw new \Exception("The id or remote id of an object or a match condition is required to $action a content version");
84
        }
85
86
        if (!isset($step->dsl['match_versions']) && !isset($step->dsl['versions'])) {
87
            throw new \Exception("A verision match condition is required to $action a content version");
88
        }
89
90
        // Backwards compat
91
92 View Code Duplication
        if (isset($step->dsl['match'])) {
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...
93
            $match = $step->dsl['match'];
94
        } else {
95
            if (isset($step->dsl['object_id'])) {
96
                $match = array('content_id' => $step->dsl['object_id']);
97
            } elseif (isset($step->dsl['remote_id'])) {
98
                $match = array('content_remote_id' => $step->dsl['remote_id']);
99
            }
100
        }
101
102
        if (isset($step->dsl['match_versions'])) {
103
            $matchVersions = $step->dsl['match_versions'];
104
        } else {
105
            $matchVersions = array(ContentVersionMatcher::MATCH_VERSION => $step->dsl['versions']);
106
        }
107
108
        // convert the references passed in the match
109
        $match = $this->resolveReferencesRecursively($match);
0 ignored issues
show
Bug introduced by
The variable $match does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
110
        $matchVersions = $this->resolveReferencesRecursively($matchVersions);
111
112
        $sort = isset($step->dsl['match_sort']) ? $this->referenceResolver->resolveReference($step->dsl['match_sort']) : array();
113
        $offset = isset($step->dsl['match_offset']) ? $this->referenceResolver->resolveReference($step->dsl['match_offset']) : 0;
114
        $limit = isset($step->dsl['match_limit']) ? $this->referenceResolver->resolveReference($step->dsl['match_limit']) : 0;
115
116
        return $this->versionMatcher->match($match, $matchVersions, $sort, $offset, $limit);
117
    }
118
119
    /**
120
     * @param VersionInfo $versionInfo
121
     * @param array $references
122
     * @param $step
123
     * @return array
124
     *
125
     * @todo allow setting more refs: creation date, modification date, creator id, langauge codes
126
     */
127
    protected function getReferencesValues($versionInfo, array $references, $step)
128
    {
129
        $refs = array();
130
131
        foreach ($references as $reference) {
132
            switch ($reference['attribute']) {
133
                case 'version_no':
134
                    $value = $versionInfo->versionNo;
135
                    break;
136
                case 'version_status':
137
                    $value = $this->versionStatusToHash($versionInfo->status);
138
                    break;
139
                default:
140
                    // NB: this will generate an error if the user tries to seta  ref to a field value
141
                    $value = reset(parent::getReferencesValues($versionInfo, array($references), $step));
0 ignored issues
show
Bug introduced by
parent::getReferencesVal...ay($references), $step) cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
Documentation introduced by
$versionInfo is of type object<eZ\Publish\API\Re...es\Content\VersionInfo>, but the function expects a object<eZ\Publish\API\Re...Values\Content\Content>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
142
            }
143
144
            $refs[$reference['identifier']] = $value;
145
        }
146
147
        return $refs;
148
    }
149
150
    protected function versionStatusToHash($status)
151
    {
152
        foreach(ContentVersionMatcher::STATUS_MAP as $own => $ez) {
153
            if ($status == $ez) {
154
                return $own;
155
            }
156
        }
157
158
        /// @todo log warning?
159
        return $status;
160
    }
161
}
162