Completed
Push — develop ( 6f71dd...40edc3 )
by Tom
11s
created

DuplicatesCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 13
c 3
b 2
f 0
lcom 1
cbo 6
dl 0
loc 124
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 25 1
C execute() 0 34 7
A getChecksums() 0 20 3
A logJUnit() 0 23 2
1
<?php
2
3
namespace N98\Magento\Command\Developer\Theme;
4
5
use N98\Magento\Command\AbstractMagentoCommand;
6
use N98\JUnitXml\Document as JUnitXmlDocument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Finder\Finder;
12
13
class DuplicatesCommand extends AbstractMagentoCommand
14
{
15
    protected function configure()
16
    {
17
        $this
18
            ->setName('dev:theme:duplicates')
19
            ->addArgument('theme', InputArgument::REQUIRED, 'Your theme')
20
            ->addArgument(
21
                'originalTheme',
22
                InputArgument::OPTIONAL,
23
                'Original theme to comapre. Default is "base/default"',
24
                'base/default'
25
            )
26
            ->addOption(
27
                'log-junit',
28
                null,
29
                InputOption::VALUE_REQUIRED,
30
                'Log duplicates in JUnit XML format to defined file.'
31
            )
32
            ->setDescription('Find duplicate files (templates, layout, locale, etc.) between two themes.')
33
        ;
34
35
        $help = <<<HELP
36
* If a filename with `--log-junit` option is set the tool generates an XML file and no output to *stdout*.
37
HELP;
38
        $this->setHelp($help);
39
    }
40
41
    /**
42
     * @param InputInterface  $input
43
     * @param OutputInterface $output
44
     *
45
     * @return int|void
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $time = microtime(true);
50
        $this->detectMagento($output);
51
        if ($this->_magentoMajorVersion == self::MAGENTO_MAJOR_VERSION_2) {
52
            $output->writeln('<error>Magento 2 is currently not supported.</error>');
53
        } else {
54
            $referenceFiles = $this->getChecksums(
55
                $this->_magentoRootFolder . '/app/design/frontend/' . $input->getArgument('originalTheme')
56
            );
57
58
            $themeFolder = $this->_magentoRootFolder . '/app/design/frontend/' . $input->getArgument('theme');
59
            $themeFiles = $this->getChecksums($themeFolder);
60
61
            $duplicates = array();
62
            foreach ($themeFiles as $themeFilename => $themeFileChecksum) {
63
                if (isset($referenceFiles[$themeFilename])
64
                    && $themeFileChecksum == $referenceFiles[$themeFilename]
65
                ) {
66
                    $duplicates[] = $themeFolder . '/' . $themeFilename;
67
                }
68
            }
69
70
            if ($input->getOption('log-junit')) {
71
                $this->logJUnit($input, $duplicates, $input->getOption('log-junit'), microtime($time) - $time);
72
            } else {
73
                if (count($duplicates) === 0) {
74
                    $output->writeln('<info>No duplicates were found</info>');
75
                } else {
76
                    $output->writeln($duplicates);
77
                }
78
            }
79
        }
80
    }
81
82
    /**
83
     * @param string $baseFolder
84
     * @return array
85
     */
86
    protected function getChecksums($baseFolder)
87
    {
88
        $finder = Finder::create();
89
        $finder
90
            ->files()
91
            ->ignoreUnreadableDirs(true)
92
            ->ignoreDotFiles(true)
93
            ->ignoreVCS(true)
94
            ->followLinks()
95
            ->in($baseFolder);
96
        $checksums = array();
97
        foreach ($finder as $file) {
98
            /* @var $file \Symfony\Component\Finder\SplFileInfo */
99
            if (file_exists($file->getRealPath())) {
100
                $checksums[$file->getRelativePathname()] = md5_file($file->getRealPath());
101
            }
102
        }
103
104
        return $checksums;
105
    }
106
107
    /**
108
     * @param InputInterface $input
109
     * @param array          $duplicates
110
     * @param string         $filename
111
     * @param float          $duration
112
     */
113
    protected function logJUnit($input, array $duplicates, $filename, $duration)
114
    {
115
        $document = new JUnitXmlDocument();
116
        $suite = $document->addTestSuite();
117
        $suite->setName('n98-magerun: ' . $this->getName());
118
        $suite->setTimestamp(new \DateTime());
119
        $suite->setTime($duration);
120
121
        $testCase = $suite->addTestCase();
122
        $testCase->setName(
123
            'Magento Duplicate Theme Files: ' . $input->getArgument('theme') . ' | ' .
124
            $input->getArgument('originalTheme')
125
        );
126
        $testCase->setClassname('ConflictsCommand');
127
        foreach ($duplicates as $duplicate) {
128
            $testCase->addFailure(
129
                sprintf('Duplicate File: %s', $duplicate),
130
                'MagentoThemeDuplicateFileException'
131
            );
132
        }
133
134
        $document->save($filename);
135
    }
136
}
137