Passed
Push — master ( 1bc6cd...cbf361 )
by Mihail
04:19
created

ClassTools::getMethodRequiredArgCount()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Ffcms\Core\Traits;
4
5
/**
6
 * Assistance tools for fast using some magical things and opportunity of classic object-oriented programming
7
 */
8
trait ClassTools
9
{
10
11
    /**
12
     * Create hash string from current class properties and itself values.
13
     * This method is good stuff for caching dynamic instances
14
     * @return string|null
15
     */
16
    public function createStringClassSnapshotHash()
17
    {
18
        $hash = null;
19
        foreach ($this as $property => $value) {
20
            $hash = md5($hash . $property . '=' . $value);
21
        }
22
        return $hash;
23
    }
24
25
    /**
26
     * Get method required arguments count
27
     * @param $class
28
     * @param string $method
29
     * @return int
30
     */
31
    public function getMethodRequiredArgCount($class, string $method): int
32
    {
33
        $instance = new \ReflectionMethod($class, $method);
34
        $count = 0;
35
        // calculate method defined arguments count
36
        foreach ($instance->getParameters() as $arg) {
37
            if (!$arg->isOptional()) {
38
                $count++;
39
            }
40
        }
41
42
        return $count;
43
    }
44
}
45