Completed
Push — master ( 5294dc...c896df )
by T
04:42
created

Visibility   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 28.99 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 70%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 11
c 3
b 0
f 1
lcom 0
cbo 0
dl 20
loc 69
ccs 14
cts 20
cp 0.7
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getMapping() 0 8 1
A get() 0 5 1
A getForContext() 0 10 3
A getModifier() 10 10 3
A toString() 10 10 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 PHPSemVerChecker\Operation;
4
5
use PhpParser\Node\Stmt;
6
use PhpParser\Node\Stmt\Class_;
7
8
class Visibility
9
{
10
	/**
11
	 * @return array
12
	 */
13 21
	public static function getMapping()
14
	{
15
		return [
16 21
			Class_::MODIFIER_PUBLIC    => 0,
17 21
			Class_::MODIFIER_PROTECTED => 1,
18 21
			Class_::MODIFIER_PRIVATE   => 2,
19 21
		];
20
	}
21
22
	/**
23
	 * @param int $visibility
24
	 * @return int
25
	 */
26 21
	public static function get($visibility)
27
	{
28 21
		$mapping = self::getMapping();
29 21
		return $mapping[$visibility];
30
	}
31
32
	/**
33
	 * @param \PhpParser\Node\Stmt $context
34
	 * @return int
35
	 */
36 21
	public static function getForContext(Stmt $context)
37
	{
38 21
		if ($context->isPublic()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhpParser\Node\Stmt as the method isPublic() does only exist in the following sub-classes of PhpParser\Node\Stmt: PhpParser\Node\Stmt\ClassMethod, PhpParser\Node\Stmt\Property. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
39 21
			return Class_::MODIFIER_PUBLIC;
40
		} elseif ($context->isProtected()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhpParser\Node\Stmt as the method isProtected() does only exist in the following sub-classes of PhpParser\Node\Stmt: PhpParser\Node\Stmt\ClassMethod, PhpParser\Node\Stmt\Property. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
41
			return Class_::MODIFIER_PROTECTED;
42
		} else {
43
			return Class_::MODIFIER_PRIVATE;
44
		}
45
	}
46
47
	/**
48
	 * @param string $visibility
49
	 * @return int
50
	 */
51 21 View Code Duplication
	public static function getModifier($visibility)
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...
52
	{
53 21
		if ($visibility === 'public') {
54 21
			return Class_::MODIFIER_PUBLIC;
55
		} elseif ($visibility === 'protected') {
56
			return Class_::MODIFIER_PROTECTED;
57
		} else {
58
			return Class_::MODIFIER_PRIVATE;
59
		}
60
	}
61
62
	/**
63
	 * @param int $visibility
64
	 * @return string
65
	 */
66 View Code Duplication
	public static function toString($visibility)
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...
67
	{
68
		if ($visibility === Class_::MODIFIER_PUBLIC) {
69
			return 'public';
70
		} elseif ($visibility === Class_::MODIFIER_PROTECTED) {
71
			return 'protected';
72
		} else {
73
			return 'private';
74
		}
75
	}
76
}
77