Passed
Push — develop ( ce2674...fc48fc )
by Paul
01:57
created

ValueHelper::invoke()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 21
nc 11
nop 2
dl 0
loc 23
rs 5.3929
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpUnitGen\Renderer\Helper;
4
5
use PhpUnitGen\Model\PropertyInterface\TypeInterface;
6
7
/**
8
 * Class ValueHelper.
9
 *
10
 * @author     Paul Thébaud <[email protected]>.
11
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
12
 * @license    https://opensource.org/licenses/MIT The MIT license.
13
 * @link       https://github.com/paul-thebaud/phpunit-generator
14
 * @since      Class available since Release 2.0.0.
15
 */
16
class ValueHelper
17
{
18
    /**
19
     * Generate a PHP value as a string for the given type (bool, int ...).
20
     *
21
     * @param int|null    $type       The type to generate value for (from TypeInterface::*).
22
     * @param string|null $customType The custom type as a string if exists.
23
     *
24
     * @return string The generated PHP value string.
25
     */
26
    public function invoke(?int $type = null, ?string $customType = null): string
27
    {
28
        switch (true) {
29
            case TypeInterface::CUSTOM === $type:
30
                return sprintf('$this->createMock(%s::class)', $customType);
31
            case TypeInterface::OBJECT === $type:
32
                return '$this->createMock(\\DateTime::class)';
33
            case TypeInterface::BOOL === $type:
34
                return 'true';
35
            case TypeInterface::INT === $type:
36
                return '42';
37
            case TypeInterface::FLOAT === $type:
38
                return '42.42';
39
            case TypeInterface::ARRAY === $type:
40
            case TypeInterface::ITERABLE === $type:
41
                return '["a", "strings", "array"]';
42
            case TypeInterface::CALLABLE === $type:
43
                return 'function(): void {/* A callable */}';
44
            case TypeInterface::STRING === $type:
45
            case TypeInterface::MIXED === $type:
46
                return '"a string to test"';
47
            default:
48
                return '/** @todo Insert a value matching type here */';
49
        }
50
    }
51
}
52