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

Object2ArrayBuilder::setReader()   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\Reader\AccessorReader;
13
use Rafrsr\LibArray2Object\Reader\PropertyReaderInterface;
14
15 View Code Duplication
class Object2ArrayBuilder 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...
16
{
17
    /**
18
     * @var PropertyReaderInterface
19
     */
20
    private $reader;
21
22
    /**
23
     * @return PropertyReaderInterface
24
     */
25
    public function getReader()
26
    {
27
        return $this->reader;
28
    }
29
30
    /**
31
     * @param PropertyReaderInterface $reader
32
     *
33
     * @return $this
34
     */
35
    public function setReader($reader)
36
    {
37
        $this->reader = $reader;
38
39
        return $this;
40
    }
41
42
    /**
43
     * Build custom Array2Object instance
44
     */
45
    public function build()
46
    {
47
        if ($this->getContext()) {
48
            $context = $this->getContext();
49
        } else {
50
            $context = new Object2ArrayContext();
51
        }
52
53
        $this->prepareContext($context);
54
55
        return new Object2Array($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\Object2ArrayContext>. 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...
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    protected function prepareContext(AbstractContext $context)
62
    {
63
        parent::prepareContext($context);
64
65
        if ($context instanceof Object2ArrayContext) {
66
            $context->setReader($this->getReader() ?: new AccessorReader());
67
        }
68
    }
69
}