1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP Deal framework |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2014, Lisachenko Alexander <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace PhpDeal\Contract; |
12
|
|
|
|
13
|
|
|
use Go\Aop\Intercept\MethodInvocation; |
14
|
|
|
use PhpDeal\Contract\Fetcher\ParentClass\MethodConditionWithInheritDocFetcher; |
15
|
|
|
use PhpDeal\Exception\ContractViolation; |
16
|
|
|
use PhpDeal\Annotation\Verify; |
17
|
|
|
|
18
|
|
|
class PreconditionContract extends Contract |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param MethodInvocation $invocation |
22
|
|
|
* @Before("@execution(PhpDeal\Annotation\Verify)") |
23
|
|
|
* @throws ContractViolation |
24
|
|
|
*/ |
25
|
19 |
|
public function check(MethodInvocation $invocation) |
26
|
|
|
{ |
27
|
19 |
|
$object = $invocation->getThis(); |
28
|
19 |
|
$args = $this->getMethodArguments($invocation); |
29
|
19 |
|
$scope = $invocation->getMethod()->getDeclaringClass()->name; |
30
|
|
|
|
31
|
19 |
|
$allContracts = $this->makeContractsUnique($this->fetchAllContracts($invocation)); |
32
|
19 |
|
$this->fulfillContracts($allContracts, $object, $scope, $args, $invocation); |
33
|
6 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param MethodInvocation $invocation |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
19 |
|
private function fetchAllContracts(MethodInvocation $invocation) |
40
|
|
|
{ |
41
|
19 |
|
$allContracts = $this->fetchParentsContracts($invocation); |
42
|
|
|
|
43
|
19 |
|
foreach ($invocation->getMethod()->getAnnotations() as $annotation) { |
44
|
19 |
|
if ($annotation instanceof Verify) { |
45
|
19 |
|
$allContracts[] = $annotation; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
19 |
|
return $allContracts; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param MethodInvocation $invocation |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
19 |
|
private function fetchParentsContracts(MethodInvocation $invocation) |
57
|
|
|
{ |
58
|
19 |
|
return (new MethodConditionWithInheritDocFetcher(Verify::class))->getConditions( |
59
|
19 |
|
$invocation->getMethod()->getDeclaringClass(), |
60
|
19 |
|
$this->reader, |
61
|
19 |
|
$invocation->getMethod()->name |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|