Completed
Push — master ( bb95e0...96b1f9 )
by Axel
05:53
created

GenerateVendorDocCommand::execute()   F

Complexity

Conditions 12
Paths 375

Size

Total Lines 77
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 54
c 0
b 0
f 0
nc 375
nop 2
dl 0
loc 77
rs 3.9458

How to fix   Long Method    Complexity   

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class GenerateVendorDocCommand extends Command
20
{
21
    protected function configure()
22
    {
23
        $this
24
            ->setName('build:generate_vendor_doc')
25
            ->setDescription('Generates a file containing all the vendors and the installed version.')
26
            ->addOption('write-to', null, InputOption::VALUE_REQUIRED, 'Where to dump the generated file.');
27
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output)
30
    {
31
        $output->writeln('Reading composer vendors.');
32
        $packages = json_decode(file_get_contents('composer.lock'), true);
33
        $packages = $packages['packages'];
34
35
        $output->writeln('Generating output');
36
37
        $typeOrder = [
38
            'zikula-module' => 'Zikula Modules',
39
            'zikula-theme' => 'Zikula Themes',
40
            'symfony-bundle' => 'Symfony Bundles',
41
            'component' => 'Web Components',
42
            'library' => 'Other PHP libraries',
43
            'composer-installer' => 'Composer Installers',
44
            'composer-plugin' => 'Composer Plugins',
45
            'symfony-pack' => 'Symfony Packages'
46
        ];
47
        $types = array_keys($typeOrder);
48
        usort($packages, function($a, $b) use ($types) {
49
            return array_search($a['type'], $types) - array_search($b['type'], $types);
50
        });
51
52
        $content = "
53
---
54
currentMenu: vendor-info
55
---
56
# Vendor information
57
";
58
        }
59
        
60
        $currentType = '';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_VARIABLE, expecting T_FUNCTION or T_CONST on line 60 at column 8
Loading history...
61
        $authors = [];
62
        foreach ($packages as $package) {
63
            if ($currentType !== $package['type']) {
64
                if ('' !== $currentType) {
65
                    $content .= "\n";
66
                }
67
                $content .= '## ' . $typeOrder[$package['type']] . "\n\n";
68
                $content .= str_repeat('-', mb_strlen($typeOrder[$package['type']])) . "\n";
69
                $currentType = $package['type'];
70
            }
71
            $content .= "- **" . $package['name'] . "** `" . $package['version'] . "`";
72
            if (isset($package['license'])) {
73
                $content .= ", License: `" . implode(', ', $package['license']) . "`\n";
74
            } else {
75
                $content .= "\n";
76
            }
77
            if (isset($package['description'])) {
78
                $content .= "  *" . $package['description'] . "*\n";
79
            }
80
            if (isset($package['authors'])) {
81
                $authors = array_merge($authors, $package['authors']);
82
            }
83
        }
84
85
        $content .= "\n\n";
86
        $content .= "These are the main authors of all of the projects supporting Zikula\n";
87
        $content .= "-------------------------------------------------------------------\n";
88
89
        $tmp = [];
90
        foreach ($authors as $k => $author) {
91
            if (in_array($author['name'], $tmp)) {
92
                unset($authors[$k]);
93
                continue;
94
            }
95
            $tmp[] = $author['name'];
96
        }
97
        foreach ($authors as $author) {
98
            $content .= "- **" . $author['name'] . "**";
99
            if (isset($author['homepage'])) {
100
                $content .= " " . $author['homepage'];
101
            }
102
            if (isset($author['email'])) {
103
                $content .= " *" . $author['email'] . "*";
104
            }
105
            $content .= "\n";
106
        }
107
108
        $output->writeln('Dumping vendors to ' . $input->getOption('write-to'));
109
110
        file_put_contents($input->getOption('write-to'), $content);
111
112
        return 0;
113
    }
114
}
115