Passed
Branch feature/code-quality (2bf2a5)
by Filipe
13:16
created

CreateDefinitionsMethods::createDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of di
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\Di\Definition;
13
14
/**
15
 * CreateDefinitionsMethods
16
 *
17
 * @package Slick\Di\Definition
18
 */
19
trait CreateDefinitionsMethods
20
{
21
22
    /**
23
     * Creates the definition for registered data
24
     *
25
     * If value is a callable then the definition is Factory, otherwise
26
     * it will create a Value definition.
27
     *
28
     * @param callable|mixed $value
29
     * @param array $parameters
30
     *
31
     * @return Value|Alias|Factory
32
     * @see Factory, Value
33
     *
34
     */
35
    protected function createDefinition(
36
        mixed $value,
37
        array $parameters = []
38
    ): Value|Alias|Factory {
39
        if (is_callable($value)) {
40
            return new Factory($value, $parameters);
41
        }
42
        return $this->createValueDefinition($value);
43
    }
44
45
    /**
46
     * Creates a definition for provided name and value pair
47
     *
48
     * If $value is a string prefixed with '@' it will create an Alias
49
     * definition. Otherwise, a Value definition will be created.
50
     *
51
     * @param mixed  $value
52
     *
53
     * @return Value|Alias
54
     */
55
    protected function createValueDefinition(mixed $value): Value|Alias
56
    {
57
        if (is_string($value) && str_contains($value, '@')) {
58
            return new Alias($value);
59
        }
60
61
        return new Value($value);
62
    }
63
}
64