NewCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 27.08%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
c 4
b 0
f 0
lcom 1
cbo 6
dl 0
loc 159
ccs 13
cts 48
cp 0.2708
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getArguments() 0 7 1
A getOptions() 0 8 1
A prepareLicence() 0 10 1
B fire() 0 31 1
A prepareComposer() 0 13 1
A prepareReadme() 0 11 1
1
<?php namespace Packedge\Workbench\Console\Package;
2
3
use Packedge\Workbench\Console\BaseCommand;
4
use Packedge\Workbench\Foundation\EventManager;
5
use Packedge\Workbench\Generators\ComposerGenerator;
6
use Packedge\Workbench\Generators\LicenceGenerator;
7
use Packedge\Workbench\Generators\PackageGenerator;
8
use Packedge\Workbench\Generators\ReadmeGenerator;
9
use Packedge\Workbench\Package;
10
use Packedge\Workbench\Parsers\PackageParser;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputOption;
13
14
class NewCommand extends BaseCommand
15
{
16
    /**
17
     * The console command name.
18
     *
19
     * @var string
20
     */
21
    protected $name = 'package:new';
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Create a new package';
28
    /**
29
     * @var Parser
30
     */
31
    protected $parser;
32
    /**
33
     * @var string
34
     */
35
    protected $packageDescription;
36
    /**
37
     * @var string
38
     */
39
    protected $packageName;
40
    /**
41
     * @var string
42
     */
43
    protected $licenceName;
44
    /**
45
     * @var Package
46
     */
47
    private $package;
48
    /**
49
     * @var EventManager
50
     */
51
    private $manager;
52
53
    /**
54
     * @param EventManager $manager
55
     * @param Package $package
56
     */
57 3
    public function __construct(EventManager $manager, Package $package = null)
58
    {
59 3
        parent::__construct();
60 3
        $this->package = $package ?: new Package;
61 3
        $this->manager = $manager;
62 3
    }
63
64
    /**
65
     * Execute the console command.
66
     *
67
     * @return void
68
     */
69
    public function fire()
70
    {
71
        // Data
72
        $this->packageName = $this->askForArgument('package', 'What is your package name?');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->askForArgument('p...is your package name?') can also be of type array. However, the property $packageName is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
73
        $this->packageDescription = $this->askForArgument('description', 'What is your package description?');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->askForArgument('d... package description?') can also be of type array. However, the property $packageDescription is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
74
        $this->licenceName = $this->chooseAnOption('Choose a licence', LicenceGenerator::showList());
0 ignored issues
show
Documentation Bug introduced by
The property $licenceName was declared of type string, but $this->chooseAnOption('C...eGenerator::showList()) is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
75
76
        $this->package->setPackageName($this->packageName);
77
78
        $this->manager->fire('package.new', $this->package);
79
80
        // Base
81
//        $this->package->setPackageName($this->packageName);
82
//        $this->parser = new PackageParser($this->packageName);
83
//        $generator = new PackageGenerator();
84
85
        // Composer
86
//        $composer = $this->prepareComposer();
87
//        $generator->addGenerator($composer);
88
89
        // Licence
90
//        $licence = $this->prepareLicence();
91
//        $generator->addGenerator($licence);
92
93
        // Readme
94
//        $readme = $this->prepareReadme();
95
//        $generator->addGenerator($readme);
96
97
        // Create the Package
98
//        $generator->create($this->package);
99
    }
100
101
    /**
102
     * Get the console command arguments.
103
     *
104
     * @return array
105
     */
106 3
    protected function getArguments()
107
    {
108
        return array(
109 3
            array('package', InputArgument::OPTIONAL, 'The name of the package to create (e.g. vendor/package-name).'),
110 3
            array('description', InputArgument::OPTIONAL, 'The description of the package.'),
111 3
        );
112
    }
113
    /**
114
     * Get the console command options.
115
     *
116
     * @return array
117
     */
118 3
    protected function getOptions()
119
    {
120
        return array(
121 3
            array('name', null, InputOption::VALUE_REQUIRED, 'The package creators name.', null),
122 3
            array('email', null, InputOption::VALUE_REQUIRED, 'The package creators email.', null),
123
//            array('force', null, InputOption::VALUE_NONE, 'Force the operation to run when the file already exists.'),
124 3
        );
125
    }
126
127
    /**
128
     * @return ComposerGenerator
129
     */
130
    protected function prepareComposer()
131
    {
132
        $composer = new ComposerGenerator;
133
        $composer->setData([
134
            'name' => $this->parser->toPackageName(),
135
            'description' => $this->packageDescription,
136
            'psr4' => $this->parser->toPsr4(), // allow overriding this.
137
            'author' => $this->option('name'), // this will be a default setting and overridden by this flag.
138
            'email' => $this->option('email'), // this will be a default setting and overridden by this flag.
139
            'licence' => $this->licenceName
140
        ]);
141
        return $composer;
142
    }
143
144
    /**
145
     * @return ReadmeGenerator
146
     */
147
    protected function prepareReadme()
148
    {
149
        $readme = new ReadmeGenerator;
150
        $readme->setData([
151
            'name' => $this->parser->toHuman(),
152
            'description' => $this->packageDescription,
153
            'package' => $this->parser->toPackageName(),
154
            'licence' => LicenceGenerator::getLicenceName($this->licenceName)
155
        ]);
156
        return $readme;
157
    }
158
159
    /**
160
     * @return LicenceGenerator
161
     */
162
    protected function prepareLicence()
163
    {
164
        $licence = new LicenceGenerator;
165
        $licence->setType($this->licenceName);
166
        $licence->setData([
167
            'name' => $this->option('name'), // this will be a default setting and overridden by this flag.
168
            'year' => date('Y'),
169
        ]);
170
        return $licence;
171
    }
172
}
173