ConverterAbstract::fieldReduce()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of gpupo/pipe2
5
 *
6
 * (c) Gilmar Pupo <[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
 * For more information, see
12
 * <https://opensource.gpupo.com/pipe2/>.
13
 */
14
15
namespace Gpupo\Pipe2\Converter;
16
17
use Gpupo\Pipe2\Traits\DocumentContainerTrait;
18
use Gpupo\Pipe2\Traits\ParserTrait;
19
20
abstract class ConverterAbstract
21
{
22
    use ParserTrait;
23
    use DocumentContainerTrait;
24
25
    protected $input;
26
    protected $output;
27
    protected $channel;
28
    protected $slug;
29
    protected $idParameters;
30
    protected $schema;
31
    protected $normalizer;
32
33
    public function __construct(Array $parameters)
34
    {
35
        $this->input = $parameters['input'];
36
        $this->output = $parameters['output'];
37
        $this->channel = $parameters['channel'];
38
        $this->slug = $parameters['slug'];
39
        $this->idParameters = $parameters['id'];
40
        $this->setSchema();
41
        $this->setNormalizer();
42
        $this->factoryDocument($parameters['formatOutput']);
43
    }
44
45
    protected function factoryDocument($formatOutput)
46
    {
47
        $this->setDocument(new Document($this->schema, $this->slug), $formatOutput);
48
49
        return $this;
50
    }
51
52
    public function getNormalizer()
53
    {
54
        return $this->normalizer;
55
    }
56
57
    public function execute()
58
    {
59
        $this->appendItens($this->parser());
60
61
        return $this;
62
    }
63
64
    protected function appendItens($list)
65
    {
66
        foreach ($list as $item) {
67
            $itemElement = $this->document->createElement('sphinx:document');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Gpupo\Pipe2\DocumentInterface as the method createElement() does only exist in the following implementations of said interface: Gpupo\Pipe2\Converter\Document, Gpupo\Pipe2\DocumentAbstract, Gpupo\Pipe2\Merge\Attributes\Document.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
68
            $id = $this->idParameters['prefix'].$item[$this->idParameters['field']];
69
            $itemElement->setAttribute('id', $id);
70
71
            if (!array_key_exists('sku', $item) || empty($item['sku'])) {
72
                $item['sku'] = $id;
73
            }
74
75
            $this->populateDocument($itemElement, $item);
76
        }
77
    }
78
79
    protected function fieldReduce(Array $item)
80
    {
81
        $list = [];
82
83
        foreach ($item as $value) {
84
            $value['tag'] = $this->schema->normalizeFieldName($value['tag']);
85
            if ($this->schema->tagInSchema($value['tag'])) {
86
                $list[] = $value;
87
            }
88
        }
89
90
        return $list;
91
    }
92
93
    protected function extended($item)
94
    {
95
        return $item;
96
    }
97
98
    protected function addSlugs(Array $item)
99
    {
100
        if (empty($this->slug)) {
101
            return $item;
102
        }
103
104
        foreach ($this->schema->getSluggables() as $key) {
105
            if (array_key_exists($key, $item)) {
106
                $item[$key.'_slug'] = $this->getNormalizer()->slugify($item[$key]);
107
            }
108
        }
109
110
        return $item;
111
    }
112
113
    protected function parser()
114
    {
115
        $list = [];
116
117
        foreach ($this->parser_create() as $data) {
118
            if ($data['tag'] !== 'item') {
119
                continue;
120
            }
121
122
            $item = array_merge(['channel' => $this->channel], $this->parserItems($data));
123
124
            $normalized = $this->getNormalizer()->normalizeArrayValues($this->schema->getKeys(), $item);
125
126
            $list[$item['id']] = $this->addSlugs($this->extended($normalized));
127
        }
128
129
        return $list;
130
    }
131
132
    protected function parser_create()
133
    {
134
        return $this->parserFromFile($this->input);
135
    }
136
}
137