MemberParserPart   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 40
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseMemberDocblock() 0 14 3
A getVisibility() 0 11 3
1
<?php
2
namespace gossi\codegen\parser\visitor\parts;
3
4
use gossi\codegen\model\AbstractPhpMember;
5
use gossi\codegen\model\PhpConstant;
6
use PhpParser\Comment\Doc;
7
use PhpParser\Node;
8
9
trait MemberParserPart {
10
11
	/**
12
	 *
13
	 * @param AbstractPhpMember|PhpConstant $member
14
	 * @param Doc $doc
15
	 */
16 9
	private function parseMemberDocblock(&$member, Doc $doc = null) {
17 9
		if ($doc !== null) {
18 3
			$member->setDocblock($doc->getReformattedText());
19 3
			$docblock = $member->getDocblock();
20 3
			$member->setDescription($docblock->getShortDescription());
21 3
			$member->setLongDescription($docblock->getLongDescription());
22
23 3
			$vars = $docblock->getTags('var');
24 3
			if ($vars->size() > 0) {
25 2
				$var = $vars->get(0);
26 2
				$member->setType($var->getType(), $var->getDescription());
27
			}
28
		}
29 9
	}
30
31
	/**
32
	 * Returns the visibility from a node
33
	 *
34
	 * @param Node $node
35
	 * @return string
36
	 */
37 8
	private function getVisibility(Node $node) {
38 8
		if ($node->isPrivate()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpParser\Node as the method isPrivate() does only exist in the following implementations of said interface: PhpParser\Node\Stmt\ClassConst, PhpParser\Node\Stmt\ClassMethod, PhpParser\Node\Stmt\Property.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
39 5
			return AbstractPhpMember::VISIBILITY_PRIVATE;
40
		}
41
42 8
		if ($node->isProtected()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpParser\Node as the method isProtected() does only exist in the following implementations of said interface: PhpParser\Node\Stmt\ClassConst, PhpParser\Node\Stmt\ClassMethod, PhpParser\Node\Stmt\Property.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
43 1
			return AbstractPhpMember::VISIBILITY_PROTECTED;
44
		}
45
46 8
		return AbstractPhpMember::VISIBILITY_PUBLIC;
47
	}
48
}
49