Completed
Push — master ( 366fbf...5f1478 )
by Rafael
02:45
created

Array2ObjectBuilder::setWriter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * LICENSE: This file is subject to the terms and conditions defined in
5
 * file 'LICENSE', which is part of this source code package.
6
 *
7
 * @copyright 2016 Copyright(c) - All rights reserved.
8
 */
9
10
namespace Rafrsr\LibArray2Object;
11
12
use Rafrsr\LibArray2Object\Parser\ObjectParser;
13
use Rafrsr\LibArray2Object\Writer\AccessorWriter;
14
use Rafrsr\LibArray2Object\Writer\PropertyWriterInterface;
15
16
/**
17
 * Class Array2ObjectBuilder
18
 */
19 View Code Duplication
class Array2ObjectBuilder extends AbstractBuilder
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
20
{
21
    /**
22
     * @var PropertyWriterInterface
23
     */
24
    private $writer;
25
26
    /**
27
     * @return PropertyWriterInterface
28
     */
29
    public function getWriter()
30
    {
31
        return $this->writer;
32
    }
33
34
    /**
35
     * @param PropertyWriterInterface $writer
36
     *
37
     * @return $this
38
     */
39
    public function setWriter($writer)
40
    {
41
        $this->writer = $writer;
42
43
        return $this;
44
    }
45
46
    /**
47
     * Build custom Array2Object instance
48
     */
49
    public function build()
50
    {
51
        if ($this->getContext()) {
52
            $context = $this->getContext();
53
        } else {
54
            $context = new Array2ObjectContext();
55
        }
56
57
        $this->prepareContext($context);
58
59
        return new Array2Object($context);
0 ignored issues
show
Compatibility introduced by
$context of type object<Rafrsr\LibArray2Object\AbstractContext> is not a sub-type of object<Rafrsr\LibArray2O...ct\Array2ObjectContext>. It seems like you assume a child class of the class Rafrsr\LibArray2Object\AbstractContext to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    protected function prepareContext(AbstractContext $context)
66
    {
67
        parent::prepareContext($context);
68
69
        if ($context instanceof Array2ObjectContext) {
70
            $context->setWriter($this->getWriter() ?: new AccessorWriter());
71
        }
72
    }
73
}