ValueHelper   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 12

1 Method

Rating   Name   Duplication   Size   Complexity  
C invoke() 0 26 12
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\Renderer\Helper;
13
14
use PhpUnitGen\Exception\Exception;
15
use PhpUnitGen\Model\PropertyInterface\TypeInterface;
16
17
/**
18
 * Class ValueHelper.
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
class ValueHelper
27
{
28
    /**
29
     * Generate a PHP value as a string for the given type (bool, int ...).
30
     *
31
     * @param int|null    $type       The type to generate value for (from TypeInterface::*).
32
     * @param string|null $customType The custom type as a string if exists.
33
     *
34
     * @return string The generated PHP value string.
35
     *
36
     * @throws Exception If a custom type does not have a custom string type (to create the mock).
37
     */
38
    public function invoke(?int $type = null, ?string $customType = null): string
39
    {
40
        switch (true) {
41
            case TypeInterface::CUSTOM === $type:
42
                if ($customType === null) {
43
                    throw new Exception('Custom type must have a custom class to mock');
44
                }
45
                return sprintf('$this->createMock(%s::class)', $customType);
46
            case TypeInterface::OBJECT === $type:
47
                return '$this->createMock(\\DateTime::class)';
48
            case TypeInterface::BOOL === $type:
49
                return 'true';
50
            case TypeInterface::INT === $type:
51
                return '42';
52
            case TypeInterface::FLOAT === $type:
53
                return '42.42';
54
            case TypeInterface::ARRAY === $type:
55
            case TypeInterface::ITERABLE === $type:
56
                return '["a", "strings", "array"]';
57
            case TypeInterface::CALLABLE === $type:
58
                return 'function(): void {/* A callable */}';
59
            case TypeInterface::STRING === $type:
60
            case TypeInterface::MIXED === $type:
61
                return '"a string to test"';
62
            default:
63
                return '/** @todo Insert a value with a correct type here */';
64
        }
65
    }
66
}
67