ClassTools   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 13
c 2
b 1
f 0
dl 0
loc 38
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createStringClassSnapshotHash() 0 10 3
A getMethodRequiredArgCount() 0 12 3
1
<?php
2
3
namespace Ffcms\Core\Traits;
4
5
use Ffcms\Core\Helper\Type\Any;
6
use Ffcms\Core\Helper\Type\Arr;
7
use Ffcms\Core\Helper\Type\Obj;
8
9
/**
10
 * Assistance tools for fast using some magical things and opportunity of classic object-oriented programming
11
 */
12
trait ClassTools
13
{
14
15
    /**
16
     * Create hash string from current class properties and itself values.
17
     * This method is good stuff for caching dynamic instances
18
     * @return string|null
19
     */
20
    public function createStringClassSnapshotHash()
21
    {
22
        $hash = null;
23
        foreach ($this as $property => $value) {
24
            if (Any::isArray($value)) {
25
                $value = implode('.', $value);
26
            }
27
            $hash = md5($hash . $property . '=' . $value);
28
        }
29
        return $hash;
30
    }
31
32
    /**
33
     * Get method required arguments count
34
     * @param $class
35
     * @param string $method
36
     * @return int
37
     */
38
    public function getMethodRequiredArgCount($class, string $method): int
39
    {
40
        $instance = new \ReflectionMethod($class, $method);
41
        $count = 0;
42
        // calculate method defined arguments count
43
        foreach ($instance->getParameters() as $arg) {
44
            if (!$arg->isOptional()) {
45
                $count++;
46
            }
47
        }
48
49
        return $count;
50
    }
51
}
52