Completed
Push — develop ( 34db25...1b1cc6 )
by Paul
02:36
created

ClassLikeTrait::hasFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpUnitGen\Model\PropertyTrait;
4
5
use Doctrine\Common\Collections\Collection;
6
use PhpUnitGen\Model\ModelInterface\FunctionModelInterface;
7
8
/**
9
 * Trait ClassLikeTrait.
10
 *
11
 * @author     Paul Thébaud <[email protected]>.
12
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
13
 * @license    https://opensource.org/licenses/MIT The MIT license.
14
 * @link       https://github.com/paul-thebaud/phpunit-generator
15
 * @since      Class available since Release 2.0.0.
16
 */
17
trait ClassLikeTrait
18
{
19
    /**
20
     * @var FunctionModelInterface[]|Collection $functions Class functions.
21
     */
22
    protected $functions;
23
24
    /**
25
     * @param FunctionModelInterface $function The function to add on this parent.
26
     */
27
    public function addFunction(FunctionModelInterface $function): void
28
    {
29
        $this->functions->add($function);
30
    }
31
32
    /**
33
     * @return FunctionModelInterface[]|Collection All the functions contained in this parent.
34
     */
35
    public function getFunctions(): Collection
36
    {
37
        return $this->functions;
38
    }
39
40
    /**
41
     * Check if a function exists.
42
     *
43
     * @param string $name The function name.
44
     *
45
     * @return bool True if it exists.
46
     */
47
    public function hasFunction(string $name): bool
48
    {
49
        return $this->functions->exists(function (FunctionModelInterface $function) use ($name) {
50
            return $function->getName() === $name;
51
        });
52
    }
53
54
    /**
55
     * Get a function if the function exists.
56
     *
57
     * @param string $name The name of the function.
58
     *
59
     * @return FunctionModelInterface|null The retrieved function, null if it does not exist.
60
     */
61
    public function getFunction(string $name): ?FunctionModelInterface
62
    {
63
        $functions = $this->functions->filter(function (FunctionModelInterface $function) use ($name) {
64
            return $function->getName() === $name;
65
        });
66
        if ($functions->isEmpty()) {
67
            return null;
68
        }
69
        return $functions->first();
70
    }
71
72
    /**
73
     * @return int The number of testable (not abstract) function in this parent.
74
     */
75
    public function countNotAbstractFunctions(): int
76
    {
77
        return $this->functions->filter(function (FunctionModelInterface $function) {
78
            return ! $function->isAbstract();
79
        })->count();
80
    }
81
}
82