Completed
Pull Request — master (#114)
by Bart
05:36
created

ExportCommand::exportToYaml()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 14
nc 6
nop 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\ConsoleCommands;
4
5
use Craft;
6
use craft\helpers\FileHelper;
7
use NerdsAndCompany\Schematic\Interfaces\MappingInterface;
8
use NerdsAndCompany\Schematic\Schematic;
9
use Symfony\Component\Yaml\Yaml;
10
11
/**
12
 * Schematic Export Command.
13
 *
14
 * Sync Craft Setups.
15
 *
16
 * @author    Nerds & Company
17
 * @copyright Copyright (c) 2015-2018, Nerds & Company
18
 * @license   MIT
19
 *
20
 * @see      http://www.nerds.company
21
 */
22
class ExportCommand extends Base
23
{
24
    /**
25
     * Exports the Craft datamodel.
26
     *
27
     * @param string $file    file to write the schema to
0 ignored issues
show
Bug introduced by
There is no parameter named $file. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
28
     * @param array  $exclude Data to not export
0 ignored issues
show
Bug introduced by
There is no parameter named $exclude. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
29
     *
30
     * @return int
31
     */
32
    public function actionIndex()
33
    {
34
        $dataTypes = $this->getDataTypes();
35
36
        $this->exportToYaml($this->file, $dataTypes);
37
        Schematic::info('Exported schema to '.$this->file);
38
39
        return 0;
40
    }
41
42
    /**
43
     * Export to Yaml file.
44
     *
45
     * @param string $file
46
     * @param bool   $autoCreate
0 ignored issues
show
Bug introduced by
There is no parameter named $autoCreate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
47
     *
48
     * @return int
49
     */
50
    public function exportToYaml($file, $dataTypes)
51
    {
52
        $result = [];
53
        foreach (array_keys($dataTypes) as $dataType) {
54
            $component = 'schematic_'.$dataType;
55
            if (Craft::$app->$component instanceof MappingInterface) {
56
                Schematic::info('Exporting '.$dataType);
57
                $result[$dataType] = Craft::$app->$component->export();
58
            } else {
59
                Schematic::error(get_class(Craft::$app->$component).' does not implement MappingInterface');
60
            }
61
        }
62
63
        $yaml = Yaml::dump($result, 10);
64
        if (!FileHelper::writeToFile($file, $yaml)) {
65
            Schematic::error('error', "Failed to write contents to \"$file\"");
0 ignored issues
show
Unused Code introduced by
The call to Schematic::error() has too many arguments starting with "Failed to write contents to \"{$file}\"".

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
66
            return 1;
67
        }
68
69
        return 0;
70
    }
71
}
72