ClassLikeTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 63
rs 10
wmc 6

5 Methods

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