Completed
Push — master ( 439ddb...32f763 )
by Peter
05:03
created

ConflictChecker   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 9
c 5
b 1
f 0
lcom 1
cbo 4
dl 0
loc 56
ccs 21
cts 21
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 16 4
B check() 0 20 5
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL, Commercial license.
5
 *
6
 * @package maslosoft/addendum
7
 * @licence AGPL, Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes)
9
 * @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes)
10
 * @copyright Copyright (c) Jan Suchal (Original version, builder, parser)
11
 * @link http://maslosoft.com/addendum/ - maslosoft addendum
12
 * @link https://code.google.com/p/addendum/ - original addendum project
13
 */
14
15
namespace Maslosoft\Addendum\Utilities;
16
17
use Maslosoft\Addendum\Collections\AnnotationsCollection;
18
use Maslosoft\Addendum\Exceptions\ConflictException;
19
use Maslosoft\Addendum\Interfaces\AnnotationInterface;
20
use Maslosoft\Addendum\Reflection\ReflectionAnnotatedClass;
21
use Maslosoft\Addendum\Reflection\ReflectionAnnotatedMethod;
22
use Maslosoft\Addendum\Reflection\ReflectionAnnotatedProperty;
23
24
/**
25
 * ConflictChecker
26
 *
27
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
28
 */
29
class ConflictChecker
30
{
31
32
	private static $_conflicts = [];
33
34
	/**
35
	 * Register annotation for later check
36
	 * @param AnnotationInterface $annotation Annotation
37
	 * @return void
38
	 */
39 45
	public static function register(AnnotationInterface $annotation)
40
	{
41 45
		$name = AnnotationName::createName($annotation);
42
43 45
		$reflection = new ReflectionAnnotatedClass($annotation);
44 45
		if ($reflection->hasAnnotation('Conflicts'))
45
		{
46 1
			$value = $reflection->getAnnotation('Conflicts')->value;
0 ignored issues
show
Bug introduced by
Accessing value 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...
47 1
			$values = is_array($value) ? $value : [$value];
48 1
			foreach($values as $secondName)
49
			{
50 1
				self::$_conflicts[$name] = $secondName;
51 1
				self::$_conflicts[$secondName] = $name;
52
			}
53
		}
54 45
	}
55
56
	/**
57
	 * Check target constraints
58
	 * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty|bool $target Target entity
59
	 * @param AnnotationsCollection $annotations
60
	 * @return void
61
	 * @throws ConflictException
62
	 */
63 46
	public static function check($target, AnnotationsCollection $annotations)
64
	{
65 46
		if (!self::$_conflicts)
0 ignored issues
show
Bug Best Practice introduced by
The expression self::$_conflicts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
66
		{
67 1
			return;
68
		}
69 46
		foreach ($annotations->getAllAnnotations() as $annotation)
70
		{
71 44
			$name = AnnotationName::createName($annotation);
72 44
			if (!isset(self::$_conflicts[$name]))
73
			{
74 27
				continue;
75
			}
76 23
			$second = self::$_conflicts[$name];
77 23
			if ($annotations->hasAnnotation($second))
78
			{
79 23
				throw new ConflictException(sprintf('Annotation `%s` cannot be used together with `%s` in `%s`', $name, $second, ReflectionName::createName($target)));
80
			}
81
		}
82 46
	}
83
84
}
85