Completed
Push — master ( eb4a71...8120d9 )
by Hu
09:57 queued 07:59
created

Utils::checkOverwriteRunMethod()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6.4228

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 31
ccs 17
cts 22
cp 0.7727
rs 8.439
cc 6
eloc 20
nc 6
nop 1
crap 6.4228
1
<?php
2
/**
3
 * @author Jenner <[email protected]>
4
 * @license https://opensource.org/licenses/MIT MIT
5
 * @datetime: 2015/11/11 17:50
6
 */
7
8
namespace Jenner\SimpleFork;
9
10
11
class Utils
12
{
13
    /**
14
     * check if the sub class of Process has overwrite the run method
15
     *
16
     * @param $child_class
17
     */
18 12
    public static function checkOverwriteRunMethod($child_class)
19
    {
20 12
        $parent_class = '\\Jenner\\SimpleFork\\Process';
21 12
        if ($child_class == $parent_class) {
22
            $message = "you should extend the `{$parent_class}`" .
23
                " and overwrite the run method";
24
            throw new \RuntimeException($message);
25
        }
26
27 12
        $child = new \ReflectionClass($child_class);
28 12
        if($child->getParentClass() === false){
29 3
            $message = "you should extend the `{$parent_class}`" .
30 3
                " and overwrite the run method";
31 3
            throw new \RuntimeException($message);
32
        }
33
34 9
        $parent_methods = $child->getParentClass()->getMethods(\ReflectionMethod::IS_PUBLIC);
35
36 9
        foreach ($parent_methods as $parent_method) {
37 9
            if ($parent_method->getName() !== 'run') continue;
38
39 9
            $declaring_class = $child->getMethod($parent_method->getName())
40 9
                ->getDeclaringClass()
41 9
                ->getName();
42
43 9
            if ($declaring_class === $parent_class) {
44
                $message = "you must overwrite the run method";
45
                throw new \RuntimeException($message);
46
            }
47 9
        }
48
    }
49
}