Passed
Push — master ( 9581f6...8a27d2 )
by Lars
12:38
created

CheckDependency::getVersionPreferenceFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MagentoHackathon\Command;
4
5
use Magento\Framework\App\Filesystem\DirectoryList;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Filesystem\DirectoryList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Magento\Framework\Filesystem;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Filesystem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use MagentoHackathon\Api\GetModulePathFolderByNameInterface;
8
use MagentoHackathon\FileCollector;
9
use MagentoHackathon\Service\CheckDependency as CheckDependencyService;
10
use MagentoHackathon\Service\GetModuleFolderByName;
11
use N98\Magento\Command\AbstractMagentoCommand;
0 ignored issues
show
Bug introduced by
The type N98\Magento\Command\AbstractMagentoCommand was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * @codeCoverageIgnore
18
 */
19
class CheckDependency extends AbstractMagentoCommand
20
{
21
    const COMMAND_NAME = 'generate:dependencies';
22
23
    const OPTION_EXT_DIR = 'extension-dir';
24
25
    const OPTION_EXT_DIR_SHORT = 'x';
26
27
    const OPTION_EXT_DIR_DECS = 'Folder name of the extension (e.g. module-catalog)';
28
29
    const OPTION_EXT_PREFERENCE = 'local-magento-dependencies';
30
31
    const OPTION_EXT_PREFERENCE_SHORT = 'l';
32
33
    const OPTION_EXT_PREFERENCE_DESC = "All extension dependencies will be generated against the local magento version. \n" .
34
    "\t e.g.  \"magento/module-customer\": \"^101.0.0\" \n" .
35
    "(If not set the version will be generated against magento 2.2 and 2.3 \n" .
36
    "e.g  \"magento/module-customer\": \"^101.0.0|^102.0.0\")";
37
38
    /**
39
     * @var CheckDependencyService
40
     */
41
    private $checkDependency;
42
    /**
43
     * @var GetModulePathFolderByNameInterface|GetModuleFolderByName
44
     */
45
    private $folderByName;
46
47
    /**
48
     * @var FileCollector
49
     */
50
    private $fileCollector;
51
52
    /**
53
     * @param CheckDependencyService $checkDependency
54
     * @param GetModuleFolderByName $folderByName
55
     */
56
    public function inject(
57
        CheckDependencyService $checkDependency,
58
        GetModuleFolderByName $folderByName,
59
        FileCollector $fileCollector
60
    ) {
61
        $this->checkDependency = $checkDependency;
62
        $this->folderByName = $folderByName;
63
        $this->fileCollector = $fileCollector;
64
    }
65
66
    protected function configure()
67
    {
68
        $this->setName('extension:generate:dependencies')
69
            ->addOption(
70
                self::OPTION_EXT_DIR,
71
                self::OPTION_EXT_DIR_SHORT,
72
                InputOption::VALUE_REQUIRED,
73
                self::OPTION_EXT_DIR_DECS
74
            )
75
            ->addOption(
76
                self::OPTION_EXT_PREFERENCE,
77
                self::OPTION_EXT_PREFERENCE_SHORT,
78
                InputOption::VALUE_NONE,
79
                self::OPTION_EXT_PREFERENCE_DESC
80
            )
81
            ->setDescription('This command will search for any soft and hard dependencies for the given extension and will generate a list of recommended composer dependencies.');
82
    }
83
84
    /**
85
     * @param InputInterface $input
86
     * @param OutputInterface $output
87
     * @return int|void
88
     */
89
    protected function execute(
90
        InputInterface $input,
91
        OutputInterface $output
92
    ) {
93
        $this->detectMagento($output);
94
        if ($this->initMagento($output)) {
95
            $moduleName = $this->getExtensionFolderName($input, $output);
96
            if ($moduleName === false) {
97
                return;
98
            }
99
100
            $modulePath = $this->folderByName->execute($moduleName);
0 ignored issues
show
Bug introduced by
It seems like $moduleName can also be of type true; however, parameter $moduleName of MagentoHackathon\Service...FolderByName::execute() does only seem to accept string, 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

100
            $modulePath = $this->folderByName->execute(/** @scrutinizer ignore-type */ $moduleName);
Loading history...
101
            if ($modulePath === null) {
102
                $output->writeln('<info>Please provide a valid module  name. </info>');
103
                return;
104
            }
105
106
            $relevantFiles = $this->fileCollector->getRelevantFiles($modulePath);
107
            $dependencyList = $this->checkDependency->execute($relevantFiles);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $dependencyList is correct as $this->checkDependency->execute($relevantFiles) targeting MagentoHackathon\Service...ckDependency::execute() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The assignment to $dependencyList is dead and can be removed.
Loading history...
108
        }
109
        return $output;
110
    }
111
112
    /**
113
     * @param InputInterface $input
114
     * @param OutputInterface $output
115
     * @return bool|string
116
     */
117
    private function getExtensionFolderName(
118
        InputInterface $input,
119
        OutputInterface $output
120
    ) {
121
        $extensionName = $input->getOption(self::OPTION_EXT_DIR);
122
123
        if (empty($extensionName)) {
124
            $output->writeln('<info>Please provide a folder name. </info>');
125
            $output->writeln('<comment>n98-magerun2 ' . self::COMMAND_NAME . ' (-' . self::OPTION_EXT_DIR . '|-' . self::OPTION_EXT_DIR_SHORT . ') FOLDER_NAME</comment>');
126
            return false;
127
        }
128
129
        $output->writeln('<info>Find extension by folder name : </info><comment>' . $extensionName . '</comment></info>');
0 ignored issues
show
Bug introduced by
Are you sure $extensionName of type boolean|string|string[] can be used in concatenation? ( Ignorable by Annotation )

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

129
        $output->writeln('<info>Find extension by folder name : </info><comment>' . /** @scrutinizer ignore-type */ $extensionName . '</comment></info>');
Loading history...
130
131
        return $extensionName;
132
    }
133
}
134