AmbientClassRunner::validateClass()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
/**
3
 * ShouldPHP
4
 *
5
 * @author  Gabriel Jacinto <[email protected]>
6
 * @status  dev
7
 * @link    https://github.com/GabrielJMJ/ShouldPHP
8
 * @license MIT
9
 */
10
 
11
namespace Gabrieljmj\Should\Runner;
12
13
use Gabrieljmj\Should\Runner\AbstractRunner;
14
use Gabrieljmj\Should\Runner\Rule\AmbientClass\AmbientClassRuleInterface;
15
use Gabrieljmj\Should\Runner\Rule\RuleInterface;
16
use Gabrieljmj\Should\Ambient\AmbientInterface;
17
18
class AmbientClassRunner extends AbstractRunner
19
{
20
    /**
21
     * Runs the tests
22
     *
23
     * @param mixed $param
24
     */
25
    public function run($param)
26
    {
27
        $class = str_replace('/', '\\', $param);
28
        $ambient = $this->validateClass($class);
29
        $ref = new \ReflectionClass($ambient);
30
        $methods = $ref->getMethods();
31
32
        foreach ($methods as $method) {
33
            if ($method->isPublic()) {
34
                if (strtolower(substr($method->name, 0, 4)) === 'test') {
35
                    call_user_func_array([$ambient, $method->name], []);
36
                }
37
            }
38
        }
39
40
        $this->runTest($ambient);
41
    }
42
43
    /**
44
     * Verifies if runner can handle something
45
     *
46
     * @param mixed $param
47
     * @return mixed
48
     */
49
    public function canHandle($param)
50
    {
51
        if (!class_exists($param)) {
52
            return class_exists(str_replace('/', '\\', $param));
53
        }
54
55
        return true;
56
    }
57
58
    /**
59
     * Validates the ambient class
60
     *
61
     * @param string $class
62
     * @return \Gabrieljmj\Should\Ambient\AmbientInterface
63
     */
64
    private function validateClass($class)
65
    {
66
        if (!class_exists($class)) {
67
            if (!class_exists(str_replace('/', '\\', $class))) {
68
                AmbientClassDoesNotExistException::trigger($class);
69
            }
70
        } elseif (!$class instanceof AmbientInterface) {
71
            //RunnerException::ambientClassDoesNotImplementAmbientInterface($class);
72
        }
73
74
        $ref = new \ReflectionClass($class);
75
        return $ref->newInstance();
76
    }
77
78
    /**
79
     * Verifies if accepts certain rule
80
     *
81
     * @param \Gabrieljmj\Should\Runner\Rule\RuleInterface $rule
82
     * @return boolean
83
     */
84
    protected function acceptRule(RuleInterface $rule)
85
    {
86
        return $rule instanceof AmbientClassRuleInterface;
87
    }
88
}