Completed
Push — master ( 56c4be...a1e3bd )
by Alexander
7s
created

AbstractContractAspect::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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\Aspect;
12
13
use Doctrine\Common\Annotations\Annotation;
14
use Doctrine\Common\Annotations\Reader;
15
use DomainException;
16
use Go\Aop\Intercept\MethodInvocation;
17
use PhpDeal\Exception\ContractViolation;
18
19
abstract class AbstractContractAspect
20
{
21
    /**
22
     * @var Reader
23
     */
24
    protected $reader;
25
26
    /**
27
     * @param Reader $reader Annotation reader
28
     */
29
    public function __construct(Reader $reader)
30
    {
31
        $this->reader = $reader;
32
    }
33
34
    /**
35
     * Returns an associative list of arguments for the method invocation
36
     *
37
     * @param MethodInvocation $invocation
38
     * @return array
39
     */
40 34
    protected function fetchMethodArguments(MethodInvocation $invocation)
41
    {
42 34
        $parameters = $invocation->getMethod()->getParameters();
43
        $argumentNames = array_map(function (\ReflectionParameter $parameter) {
44 33
            return $parameter->name;
45 34
        }, $parameters);
46 34
        $parameters = array_combine($argumentNames, $invocation->getArguments());
47
48 34
        return $parameters;
49
    }
50
51
    /**
52
     * Performs verification of contracts for given invocation
53
     *
54
     * @param MethodInvocation $invocation Current invocation
55
     * @param array|Annotation[] $contracts Contract annotation
56
     * @param object|string $instance Invocation instance or string for static class
57
     * @param string $scope Scope of method
58
     * @param array $args List of arguments for the method
59
     *
60
     * @throws DomainException
61
     */
62 34
    protected function ensureContracts(MethodInvocation $invocation, array $contracts, $instance, $scope, array $args)
63
    {
64 34
        static $invoker = null;
65 34
        if (!$invoker) {
66 34
            $invoker = function () {
67 34
                extract(func_get_arg(0));
0 ignored issues
show
Bug introduced by
func_get_arg(0) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
68
69 34
                return eval('return ' . func_get_arg(1) . '; ?>');
70 3
            };
71
        }
72
73 34
        $instance     = is_object($instance) ? $instance : null;
74 34
        $boundInvoker = $invoker->bindTo($instance, $scope);
75
76 34
        foreach ($contracts as $contract) {
77 34
            $contractExpression = $contract->value;
78
            try {
79 34
                $invocationResult = $boundInvoker->__invoke($args, $contractExpression);
80
81
                // we accept as a result only true or null
82
                // null may be a result of assertions from beberlei/assert which passed
83 30
                if ($invocationResult !== null && $invocationResult !== true) {
84
                    $errorMessage = 'Invalid return value received from the assertion body,'
85 19
                        . ' only boolean or void can be returned';
86 30
                    throw new DomainException($errorMessage);
87
                }
88
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
89 23
            } catch (\Error $internalError) {
0 ignored issues
show
Bug introduced by
The class Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
90
                // PHP-7 friendly interceptor for fatal errors
91
                throw new ContractViolation($invocation, $contractExpression, $internalError);
92 23
            } catch (\Exception $internalException) {
93 34
                throw new ContractViolation($invocation, $contractExpression, $internalException);
94
            }
95
        }
96 12
    }
97
}
98