Completed
Push — master ( f46373...d10f54 )
by
unknown
04:56
created

DefinitionBuilder::toMetadataCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by PhpStorm.
4
 * User: root
5
 * Date: 02.08.16
6
 * Time: 0:46.
7
 */
8
namespace samsonframework\container\definition;
9
10
use samsonframework\container\exception\ClassDefinitionAlreadyExistsException;
11
12
/**
13
 * Class DefinitionBuilder
14
 *
15
 * @author Ruslan Molodyko <[email protected]>
16
 */
17
class DefinitionBuilder extends AbstractDefinition
18
{
19
    /** @var  ClassDefinition[] Definition collection */
20
    protected $definitionCollection = [];
21
22
    /**
23
     * Add new class definition
24
     *
25
     * @param $className
26
     * @param string $serviceName
27
     * @return ClassBuilderInterface
28
     * @throws ClassDefinitionAlreadyExistsException
29
     */
30 5
    public function addDefinition($className, string $serviceName = null): ClassBuilderInterface
31
    {
32
        // Check if class already exists
33 5
        if (array_key_exists($className, $this->definitionCollection)) {
34
            throw new ClassDefinitionAlreadyExistsException();
35
        }
36
37
        // Create new definition
38 5
        $classDefinition = new ClassDefinition($this);
39 5
        $classDefinition->setClassName($className);
40 5
        if ($serviceName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $serviceName of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
41 2
            $classDefinition->setServiceName($serviceName);
42
        }
43
44
        // Register definition
45 5
        $this->definitionCollection[$className] = $classDefinition;
46
47 5
        return $classDefinition;
48
    }
49
50
    /**
51
     * Convert to metadata collection
52
     *
53
     * @return array
54
     */
55
    public function toMetadataCollection(): array
56
    {
57
        $metadataCollection = [];
58
        foreach ($this->definitionCollection as $classDefinition) {
59
            $metadataCollection[$classDefinition->getClassName()] = $classDefinition->toMetadata();
60
        }
61
62
        return $metadataCollection;
63
    }
64
}
65