Completed
Push — master ( fc6e2c...9b1d08 )
by Joseph
02:29
created

Meta   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 20.83 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
c 1
b 0
f 1
lcom 0
cbo 0
dl 10
loc 48
ccs 33
cts 33
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A og() 5 9 3
A twitter() 5 11 3
A unFlatten() 0 13 3
A deepen() 0 9 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
3
namespace Jclyons52\PagePreview;
4
5
class Meta extends \ArrayObject
6
{
7 3
    public function og()
8
    {
9 3 View Code Duplication
        foreach ($this as $key => $value) {
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...
10 3
            if (strpos($key, 'og:') > -1) {
11 3
                $result[$key] = $value;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
12 3
            }
13 3
        }
14 3
        return $result;
0 ignored issues
show
Bug introduced by
The variable $result 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...
15
    }
16
17 3
    public function twitter()
18
    {
19 3
        $result = [];
20 3 View Code Duplication
        foreach ($this as $key => $value) {
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...
21 3
            if (strpos($key, 'twitter:') > -1) {
22 3
                $result[$key] = $value;
23 3
            }
24 3
        }
25 3
        $result = $this->unFlatten($result);
26 3
        return $result['twitter'];
27
    }
28
29 6
    public function unFlatten($array = null)
30
    {
31 6
        if ($array === null) {
32 3
            $array = $this;
33 3
        }
34 6
        $result = [];
35 6
        foreach ($array as $key => $value) {
36 6
                $keys = explode(':', $key);
37 6
                $mu = $this->deepen($keys, $value);
38 6
                $result = array_merge_recursive($result, $mu);
39 6
        }
40 6
        return $result;
41
    }
42
43 6
    public function deepen($keys, $value, $index = 0, $carry = [])
44
    {
45 6
        if (array_key_exists($index + 1, $keys)) {
46 6
            $carry[$keys[$index]] = $this->deepen($keys, $value, $index + 1);
47 6
        } else {
48 6
            $carry[$keys[$index]] = $value;
49
        }
50 6
        return $carry;
51
    }
52
}
53