Completed
Push — master ( e560bc...809be1 )
by Peter
05:23
created

ExceptionFormatter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B formatForAnnotation() 0 37 3
1
<?php
2
3
namespace Maslosoft\Signals\Helpers;
4
5
use Maslosoft\Addendum\Interfaces\AnnotationInterface;
6
use ReflectionClass;
7
8
/**
9
 * ExceptionFormatter
10
 *
11
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
12
 */
13
class ExceptionFormatter
14
{
15
16
	public static function formatForAnnotation(AnnotationInterface $annotation, $class)
17
	{
18
		$shortName = (new ReflectionClass($annotation))->getShortName();
19
		$type = preg_replace('~Annotation$~', '', $shortName);
20
		$typeName = $annotation->getMeta()->type()->name;
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Maslosoft\Addendum\Interfaces\AnnotationInterface as the method getMeta() does only exist in the following implementations of said interface: Maslosoft\Addendum\Annotations\ConflictsAnnotation, Maslosoft\Addendum\Collections\MetaAnnotation, Maslosoft\Signals\Meta\SignalsAnnotation, Maslosoft\Signals\SignalForAnnotation, Maslosoft\Signals\SlotForAnnotation.

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...
21
		$name = $annotation->name;
0 ignored issues
show
Bug introduced by
Accessing name on the interface Maslosoft\Addendum\Interfaces\AnnotationInterface 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
		// Always available params
24
		$params = [
25
			$type,
26
			$typeName,
27
		];
28
29
		// Prepare first part of message
30
		if (empty($class))
31
		{
32
			$msg = 'Could not resolve class name';
33
		}
34
		else
35
		{
36
			array_unshift($params, $class);
37
			$msg = 'Class `%s` not found';
38
		}
39
40
		// Prepare location of bogus annotation
41
		if ($name === $typeName)
42
		{
43
			$msgOn = 'for `@%s` annotation on `%s`';
44
		}
45
		else
46
		{
47
			$params[] = $name;
48
			$msgOn = 'for `@%s` annotation on `%s::%s`';
49
		}
50
		$msg = implode(' ', [$msg, $msgOn]);
51
		return vsprintf($msg, $params);
52
	}
53
54
}
55