FactoryParser   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 40.82 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 34.88%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 40
loc 98
ccs 15
cts 43
cp 0.3488
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 6 1
B checkFactory() 20 24 3
B parseFactory() 20 27 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the LaravelYaml package.
5
 *
6
 * (c) Théo FIDRY <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Fidry\LaravelYaml\FileLoader\Parser\Yaml\Util;
13
14
use Fidry\LaravelYaml\DependencyInjection\Definition\FactoryInterface;
15
use Fidry\LaravelYaml\DependencyInjection\Definition\Factory;
16
use Fidry\LaravelYaml\DependencyInjection\Definition\ServiceInterface;
17
use Fidry\LaravelYaml\Exception\FileLoader\InvalidArgumentException;
18
use Fidry\LaravelYaml\FileLoader\Parser\Resolver\ResolverInterface;
19
20
/**
21
 * @author Théo FIDRY <[email protected]>
22
 */
23
final class FactoryParser
24
{
25
    /**
26
     * @var ResolverInterface
27
     */
28
    private $serviceResolver;
29
30 44
    public function __construct(ResolverInterface $serviceResolver)
31
    {
32 44
        $this->serviceResolver = $serviceResolver;
33 44
    }
34
35
    /**
36
     * Parses a factory service definition and return the factory object.
37
     *
38
     * @param ServiceInterface $service
39
     * @param mixed            $factory
40
     * @param string           $fileName file name
41
     *
42
     * @return ServiceInterface
43
     * @throws InvalidArgumentException
44
     */
45 2
    public function parse(ServiceInterface $service, $factory, $fileName)
46
    {
47 2
        $this->checkFactory($service, $factory, $fileName);
48
49 2
        return $this->parseFactory($service, $factory[0], $factory[1], $fileName);
50
    }
51
52
    /**
53
     * @param ServiceInterface $service
54
     * @param mixed            $factory
55
     * @param string           $fileName file name
56
     *
57
     * @throws InvalidArgumentException
58
     */
59 2
    private function checkFactory(ServiceInterface $service, $factory, $fileName)
60
    {
61 2 View Code Duplication
        if (false === is_array($factory)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
            throw new InvalidArgumentException(
63
                sprintf(
64
                    'Parameter "factory" must be an array for service "%s", but found "%s" instead in %s. Check your YAML syntax.',
65
                    $service->getName(),
66
                    gettype($factory),
67
                    $fileName
68
                )
69
            );
70
        }
71
72 2 View Code Duplication
        if (2 !== count($factory)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
            throw new InvalidArgumentException(
74
                sprintf(
75
                    'Parameter "factory" expects two parameters for service "%s", but found "%s" instead in %s. Check your YAML syntax.',
76
                    $service->getName(),
77
                    gettype($factory),
78
                    $fileName
79
                )
80
            );
81
        }
82 2
    }
83
84
    /**
85
     * @param ServiceInterface $service
86
     * @param mixed            $factoryClass
87
     * @param mixed            $factoryMethod
88
     * @param string           $fileName file name
89
     *
90
     * @return FactoryInterface
91
     * @throws InvalidArgumentException
92
     */
93 2
    private function parseFactory(ServiceInterface $service, $factoryClass, $factoryMethod, $fileName)
94
    {
95 2 View Code Duplication
        if (false === is_string($factoryClass)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            throw new InvalidArgumentException(
97
                sprintf(
98
                    'The first parameter of "factory" for service "%s" must be a class name or a reference to a service, but found "%s" instead in %s. Check your YAML syntax.',
99
                    $service->getName(),
100
                    gettype($factoryClass),
101
                    $fileName
102
                )
103
            );
104
        }
105 2
        $factoryClass = $this->serviceResolver->resolve($factoryClass);
106
107 2 View Code Duplication
        if (false === is_string($factoryMethod)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
            throw new InvalidArgumentException(
109
                sprintf(
110
                    'The second parameter of "factory" for service "%s" must be a class name or a reference to a service, but found "%s" instead in %s. Check your YAML syntax.',
111
                    $service->getName(),
112
                    gettype($factoryMethod),
113
                    $fileName
114
                )
115
            );
116
        }
117
118 2
        return new Factory($service, $factoryClass, $factoryMethod);
119
    }
120
}
121