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

ClassTools   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 35
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createStringClassSnapshotHash() 0 7 2
A getMethodRequiredArgCount() 0 12 3
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