Completed
Push — master ( 2bd329...4a0c8e )
by Gaetano
18:18 queued 15:28
created

ContentVersionManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.45%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 49
ccs 21
cts 22
cp 0.9545
rs 10
c 2
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C delete() 0 40 8
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
6
7
/**
8
 * Handles content-version migrations.
9
 */
10
class ContentVersionManager extends ContentManager
11
{
12
    protected $supportedStepTypes = array('content_version');
13
    protected $supportedActions = array('delete');
14
15
    /**
16
     * Handles the content delete migration action type
17
     */
18 1
    protected function delete($step)
19
    {
20 1
        if (!isset($step->dsl['versions'])) {
21
            throw new \Exception("The 'versions' tag is required to delete content versions");
22
        }
23
24 1
        $contentCollection = $this->matchContents('delete', $step);
25
26 1
        $this->setReferences($contentCollection, $step);
0 ignored issues
show
Bug introduced by
It seems like $contentCollection defined by $this->matchContents('delete', $step) on line 24 can be null; however, Kaliop\eZMigrationBundle...anager::setReferences() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
27
28 1
        $contentService = $this->repository->getContentService();
29 1
        $versions = (array)$step->dsl['versions'];
30
31 1
        foreach ($contentCollection as $content) {
0 ignored issues
show
Bug introduced by
The expression $contentCollection of type object<Kaliop\eZMigratio...ContentCollection>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
32 1
            foreach($versions as $versionId) {
33
                try {
34 1
                    if ($versionId < 0) {
35 1
                        $contentVersions = $contentService->loadVersions($content->contentInfo);
36
                        // different eZ kernels apparently sort versions in different order...
37 1
                        $sortedVersions = array();
38 1
                        foreach($contentVersions as $versionInfo) {
39 1
                            $sortedVersions[$versionInfo->versionNo] = $versionInfo;
40
                        }
41 1
                        ksort($sortedVersions);
42 1
                        $sortedVersions = array_slice($sortedVersions, 0, $versionId);
43 1
                        foreach($sortedVersions as $versionInfo) {
44 1
                            $contentService->deleteVersion($versionInfo);
45
                        }
46
                    } else {
47 1
                        $versionInfo = $contentService->loadVersionInfo($content->contentInfo, $versionId);
48 1
                        $contentService->deleteVersion($versionInfo);
49
                    }
50 1
                } catch (NotFoundException $e) {
51
                    // Someone else removed the content version. We can safely ignore this
52
                }
53
            }
54
        }
55
56 1
        return $contentCollection;
57
    }
58
}
59