Completed
Push — master ( c896df...0e1c32 )
by T
04:38
created

Visibility::toString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
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 80
	public static function getMapping()
14
	{
15
		return [
16 80
			Class_::MODIFIER_PUBLIC    => 0,
17 80
			Class_::MODIFIER_PROTECTED => 1,
18 80
			Class_::MODIFIER_PRIVATE   => 2,
19 80
		];
20
	}
21
22
	/**
23
	 * @param int $visibility
24
	 * @return int
25
	 */
26 80
	public static function get($visibility)
27
	{
28 80
		$mapping = self::getMapping();
29 80
		return $mapping[$visibility];
30
	}
31
32
	/**
33
	 * @param \PhpParser\Node\Stmt $context
34
	 * @return int
35
	 */
36 80
	public static function getForContext(Stmt $context)
37
	{
38 80
		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 36
			return Class_::MODIFIER_PUBLIC;
40 44
		} 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 22
			return Class_::MODIFIER_PROTECTED;
42
		} else {
43 22
			return Class_::MODIFIER_PRIVATE;
44
		}
45
	}
46
47
	/**
48
	 * @param string $visibility
49
	 * @return int
50
	 */
51 97 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 97
		if ($visibility === 'public') {
54 41
			return Class_::MODIFIER_PUBLIC;
55 56
		} elseif ($visibility === 'protected') {
56 28
			return Class_::MODIFIER_PROTECTED;
57
		} else {
58 28
			return Class_::MODIFIER_PRIVATE;
59
		}
60
	}
61
62
	/**
63
	 * @param int $visibility
64
	 * @return string
65
	 */
66 80 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 80
		if ($visibility === Class_::MODIFIER_PUBLIC) {
69 36
			return 'public';
70 44
		} elseif ($visibility === Class_::MODIFIER_PROTECTED) {
71 22
			return 'protected';
72
		} else {
73 22
			return 'private';
74
		}
75
	}
76
}
77