1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isolate\UnitOfWork\Entity\Property; |
4
|
|
|
|
5
|
|
|
use Isolate\UnitOfWork\Object\PropertyAccessor; |
6
|
|
|
use Isolate\UnitOfWork\Entity\Definition\Property; |
7
|
|
|
use Isolate\UnitOfWork\Exception\InvalidArgumentException; |
8
|
|
|
use SebastianBergmann\Comparator\ComparisonFailure; |
9
|
|
|
use SebastianBergmann\Comparator\Factory; |
10
|
|
|
|
11
|
|
|
final class PHPUnitValueComparer implements ValueComparer |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var PropertyAccessor |
15
|
|
|
*/ |
16
|
|
|
private $propertyAccessor; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Factory |
20
|
|
|
*/ |
21
|
|
|
private $comparatorFactory; |
22
|
|
|
|
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->comparatorFactory = new Factory(); |
26
|
|
|
$this->propertyAccessor = new PropertyAccessor(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Property $property |
31
|
|
|
* @param $firstObject |
32
|
|
|
* @param $secondObject |
33
|
|
|
* @return bool |
34
|
|
|
* @throws InvalidArgumentException |
35
|
|
|
*/ |
36
|
|
|
public function hasDifferentValue(Property $property, $firstObject, $secondObject) |
37
|
|
|
{ |
38
|
|
|
$this->validaObjects($firstObject, $secondObject); |
39
|
|
|
|
40
|
|
|
$firstValue = $this->propertyAccessor->getValue($firstObject, $property->getName()); |
41
|
|
|
$secondValue = $this->propertyAccessor->getValue($secondObject, $property->getName()); |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
$comparator = $this->comparatorFactory->getComparatorFor($firstValue, $secondValue); |
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
$comparator->assertEquals($firstValue, $secondValue); |
48
|
|
|
return false; |
49
|
|
|
} catch (ComparisonFailure $exception) { |
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $firstObject |
56
|
|
|
* @param $secondObject |
57
|
|
|
* @throws InvalidArgumentException |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
private function validaObjects($firstObject, $secondObject) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
if (!is_object($firstObject) || !is_object($secondObject)) { |
62
|
|
|
throw new InvalidArgumentException("Compared values need to be a valid objects."); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (get_class($firstObject) !== get_class($secondObject)) { |
66
|
|
|
throw new InvalidArgumentException("Compared values need to be an instances of the same class."); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
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.