Completed
Push — add/going-backwards ( 3440c3 )
by
unknown
448:15 queued 439:43
created

Compatibility   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 11.11 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 6
loc 54
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add_feature() 3 9 4
A can_use() 0 9 3
A add_plugin() 3 5 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
 * The Backward Compatibility class.
4
 *
5
 * @package automattic/jetpack-back
6
 */
7
8
namespace Automattic\Jetpack\Back;
9
10
/**
11
 * The class manages the features supported by the plugins.
12
 */
13
class Compatibility {
14
15
	/**
16
	 * Supported features are stored here on per-plugin basis.
17
	 *
18
	 * @var array
19
	 */
20
	private static $features = array();
21
22
	/**
23
	 * Mark a feature as supported by the plugin.
24
	 *
25
	 * @param string $plugin The plugin slug.
26
	 * @param string $feature The feature label.
27
	 */
28
	public static function add_feature( $plugin, $feature ) {
29 View Code Duplication
		if ( ! array_key_exists( $plugin, static::$features ) || ! is_array( static::$features[ $plugin ] ) ) {
0 ignored issues
show
Bug introduced by
Since $features is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $features to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
30
			static::$features[ $plugin ] = array();
0 ignored issues
show
Bug introduced by
Since $features is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $features to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
31
		}
32
33
		if ( ! in_array( $feature, static::$features[ $plugin ], true ) ) {
0 ignored issues
show
Bug introduced by
Since $features is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $features to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
34
			static::$features[ $plugin ][] = $feature;
0 ignored issues
show
Bug introduced by
Since $features is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $features to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
35
		}
36
	}
37
38
	/**
39
	 * Check if all the plugins support a feature.
40
	 *
41
	 * @param string $feature The feature label.
42
	 *
43
	 * @return bool True if the feature is supported, false otherwise.
44
	 */
45
	public static function can_use( $feature ) {
46
		foreach ( static::$features as $features ) {
0 ignored issues
show
Bug introduced by
Since $features is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $features to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
47
			if ( ! in_array( $feature, $features, true ) ) {
48
				return false;
49
			}
50
		}
51
52
		return true;
53
	}
54
55
	/**
56
	 * Let the package know that the plugin exists and needs to be checked for compatibility.
57
	 *
58
	 * @param string $plugin The plugin slug.
59
	 */
60
	public static function add_plugin( $plugin ) {
61 View Code Duplication
		if ( empty( static::$features[ $plugin ] ) || ! is_array( static::$features[ $plugin ] ) ) {
0 ignored issues
show
Bug introduced by
Since $features is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $features to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
62
			static::$features[ $plugin ] = array();
0 ignored issues
show
Bug introduced by
Since $features is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $features to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
63
		}
64
	}
65
66
}
67