Completed
Push — master ( a773d5...ab9ae9 )
by Marc
02:29
created

TripleTagSet::convertStringToTagSet()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 32
Code Lines 19

Duplication

Lines 10
Ratio 31.25 %

Code Coverage

Tests 23
CRAP Score 5

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 10
loc 32
ccs 23
cts 23
cp 1
rs 8.439
cc 5
eloc 19
nc 6
nop 1
crap 5
1
<?php
2
3
/*
4
 * This file is part of the kaloa/metadata package.
5
 *
6
 * For full copyright and license information, please view the LICENSE file
7
 * that was distributed with this source code.
8
 */
9
10
namespace Kaloa\Metadata;
11
12
use Kaloa\Metadata\TripleTag;
13
use Kaloa\Util\AbstractSet;
14
15
/**
16
 *
17
 */
18
final class TripleTagSet extends AbstractSet
19
{
20
    protected $_managedClass = 'Kaloa\\Metadata\\TripleTag';
21
22
    /**
23
     *
24
     * @param TripleTagSet
25
     * @param TripleTagSet
26
     * @return array
27
     */
28 5
    public static function calculateTransformations(
29
        TripleTagSet $oldSet = null,
30
        TripleTagSet $newSet = null
31
    ) {
32
        $transformations = array(
33 5
            'keep'   => array(),
34 5
            'add'    => array(),
35 5
            'delete' => array()
36 5
        );
37
38 5
        if (null === $oldSet && null === $newSet) {
39 1
            return $transformations;
40
        }
41
42 5
        if (null === $oldSet) {
43 2
            foreach ($newSet as $newTag) {
0 ignored issues
show
Bug introduced by
The expression $newSet of type null|object<Kaloa\Metadata\TripleTagSet> 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...
44 1
                $transformations['add'][] = $newTag;
45 2
            }
46
47 2
            return $transformations;
48
        }
49
50 5
        if (null === $newSet) {
51 2
            foreach ($oldSet as $oldTag) {
52 1
                $transformations['delete'][] = $oldTag;
53 2
            }
54
55 2
            return $transformations;
56
        }
57
58 4
        foreach ($newSet as $newTag) {
59 3
            foreach ($oldSet as $oldTag) {
60 3
                if ($newTag->equals($oldTag)) {
61
                    // Tags in ($newSet AND $oldSet)
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62 2
                    $transformations['keep'][] = $newTag;
63 2
                    continue 2;
64
                }
65 2
            }
66
67
            // Tags in ($newSet AND NOT $oldSet)
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
68 2
            $transformations['add'][] = $newTag;
69 4
        }
70
71 4
        foreach ($oldSet as $oldTag) {
72 3
            foreach ($newSet as $newTag) {
73 3
                if ($oldTag->equals($newTag)) {
74 2
                    continue 2;
75
                }
76 2
            }
77
78
            // Tags in ($oldSet AND NOT $newSet)
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79 2
            $transformations['delete'][] = $oldTag;
80 4
        }
81
82 4
        return $transformations;
83
    }
84
85
    /**
86
     * Transforms a string of linebreak-separated triple tags into a tag array
87
     * with namespace/predicate/value entries
88
     *
89
     * @param  string $s string of triple tags
90
     * @return TripleTagSet array of TripleTag
91
     */
92 1
    public static function convertStringToTagSet($s)
93
    {
94 1
        $tags = new TripleTagSet();
95
96 1
        $s = str_replace(array("\r\n", "\r"), array("\n", "\n"), $s);
97
98 1
        $tmp = explode("\n", $s);
99 1
        foreach ($tmp as $t) {
100 1
            $newTag = new TripleTag();
101
102 1
            $t = trim($t);
103 1
            if ('' !== $t) {
104 1 View Code Duplication
                if (false !== strpos($t, ':')) {
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...
105 1
                    $parts = explode(':', $t, 2);
106 1
                    $newTag->setNamespace($parts[0]);
107 1
                    $t = $parts[1];
108 1
                }
109
110 1 View Code Duplication
                if (false !== strpos($t, '=')) {
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...
111 1
                    $parts = explode('=', $t, 2);
112 1
                    $newTag->setPredicate($parts[0]);
113 1
                    $t = $parts[1];
114 1
                }
115
116 1
                $newTag->setValue($t);
117
118 1
                $tags->add($newTag);
119 1
            }
120 1
        }
121
122 1
        return $tags;
123
    }
124
125
    /**
126
     *
127
     * @param array $tags
128
     * @return TripleTagSet
129
     */
130 1
    public static function convertArrayToTagSet(array $tags)
131
    {
132 1
        $tagSet = new TripleTagSet();
133
134 1
        foreach ($tags as $tag) {
135 1
            $newTag = new TripleTag(
136 1
                $tag['value'],
137 1
                $tag['predicate'],
138 1
                $tag['namespace']
139 1
            );
140
141 1
            $tagSet->add($newTag);
142 1
        }
143
144 1
        return $tagSet;
145
    }
146
}
147