Passed
Push — 8.x ( e0db1c...0e6b2e )
by Tim
03:21
created

ImportConvertValueCommand::executeSimpleCommand()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 55
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 22
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 55
rs 9.2568

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Command\ImportConvertValueCommand
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-cli-simple
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Cli\Command;
22
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputArgument;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use TechDivision\Import\Utils\CommandNames;
27
use TechDivision\Import\ConfigurationInterface;
28
use TechDivision\Import\Serializers\ValueCsvSerializer;
29
30
/**
31
 * The command to simulate converting a file.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2019 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-cli-simple
37
 * @link      http://www.techdivision.com
38
 */
39
class ImportConvertValueCommand extends AbstractSimpleImportCommand
40
{
41
42
    /**
43
     * Configures the current command.
44
     *
45
     * @return void
46
     * @see \Symfony\Component\Console\Command\Command::configure()
47
     */
48
    protected function configure()
49
    {
50
51
        // initialize the command with the required/optional options
52
        $this->setName(CommandNames::IMPORT_CONVERT_VALUE)
53
             ->setDescription('Converts the value to the format expected by the given column')
54
             ->addArgument(InputArgumentKeys::ENTITY_TYPE_CODE, InputArgument::REQUIRED, 'The entity type code to use, e. g. catalog_product')
55
             ->addArgument(InputArgumentKeys::COLUMN, InputArgument::REQUIRED, 'The column name to convert the value for')
56
             ->addArgument(InputArgumentKeys::VALUES, InputArgument::REQUIRED|InputArgument::IS_ARRAY, 'The value to convert');
57
58
        // invoke the parent method
59
        parent::configure();
60
    }
61
62
    /**
63
     * Finally executes the simple command.
64
     *
65
     * @param \TechDivision\Import\ConfigurationInterface       $configuration The configuration instance
66
     * @param \Symfony\Component\Console\Input\InputInterface   $input         An InputInterface instance
67
     * @param \Symfony\Component\Console\Output\OutputInterface $output        An OutputInterface instance
68
     *
69
     * @return void
70
     */
71
    protected function executeSimpleCommand(
72
        ConfigurationInterface $configuration,
73
        InputInterface $input,
74
        OutputInterface $output
75
    ) {
76
77
        // initialize the default CSV serializer
78
        $serializer = new ValueCsvSerializer();
79
        $serializer->init($configuration);
80
81
        // initialize the array for the values that has to be serialized
82
        $serialize = array();
83
84
        // load the values that has to be serialized
85
        $values = $input->getArgument(InputArgumentKeys::VALUES);
86
87
        // simulate custom column handling
88
        switch ($input->getArgument(InputArgumentKeys::COLUMN)) {
89
90
            case 'categories':
91
                // serialize the categories and use the default delimiter
92
                $serialize[] = $serializer->serialize($values);
0 ignored issues
show
Bug introduced by
It seems like $values can also be of type string; however, parameter $unserialized of TechDivision\Import\Seri...Serializer::serialize() does only seem to accept array|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
                $serialize[] = $serializer->serialize(/** @scrutinizer ignore-type */ $values);
Loading history...
93
                break;
94
95
            case 'path':
96
                // categories use a slash (/) as delimiter for the first level of serialization
97
                $delimiter = '/';
98
                // load the enclosure from the configuration
99
                $enclosure = $configuration->getEnclosure();
100
                // iterate over the values and serialize them
101
                for ($i = 0; $i < sizeof($values); $i++) {
0 ignored issues
show
Bug introduced by
It seems like $values can also be of type string; however, parameter $var of sizeof() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
                for ($i = 0; $i < sizeof(/** @scrutinizer ignore-type */ $values); $i++) {
Loading history...
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
102
                    // serialize the value and use a slash (/) as delimiter
103
                    $val = $serializer->serialize(array($values[$i]), $delimiter);
104
105
                    // clean-up (means to remove surrounding + double quotes)
106
                    // because we've no delimiter within the value
107
                    if (strstr($val, $delimiter) === false) {
108
                        $val = preg_replace("/^(\'(.*)\'|${enclosure}(.*)${enclosure})$/", '$2$3', $val);
109
                        $val = str_replace('""', '"', $val);
110
                    }
111
112
                    // append the cleaned value
113
                    $values[$i] = $val;
114
                }
115
116
                // implode the category's to get the complete path
117
                $serialize[] = implode($delimiter, $values);
0 ignored issues
show
Bug introduced by
It seems like $values can also be of type null and string; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
                $serialize[] = implode($delimiter, /** @scrutinizer ignore-type */ $values);
Loading history...
118
                break;
119
120
            default:
121
                break;
122
        }
123
124
        // second serialization that simulates the framework parsing the CSV file
125
        $output->write($serializer->serialize($serialize));
126
    }
127
}
128