Completed
Pull Request — master (#299)
by Atlas
03:31
created

MetricClassNameGenerator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A getName() 0 6 3
1
<?php
2
3
namespace Hal\Metric\Helper;
4
5
use PhpParser\Node;
6
7
/**
8
 * Performs
9
 */
10
class MetricClassNameGenerator
11
{
12
    /**
13
     * @param Node|Node\Stmt\Class_|Node\Stmt\Interface_ $node
14
     *
15
     * @return string
16
     */
17
    public static function getName(Node $node)
18
    {
19
        return ($node instanceof Node\Stmt\Class_ && $node->isAnonymous()) ?
20
            'anonymous@' . spl_object_hash($node) :
21
            $node->namespacedName->toString();
0 ignored issues
show
Bug introduced by
Accessing namespacedName on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
22
    }
23
}
24