Completed
Pull Request — master (#74)
by Tim
14:37 queued 05:46
created

RoboFile::createPhar()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 85
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 8.6875
c 0
b 0
f 0
cc 3
eloc 47
nc 4
nop 0

How to fix   Long Method   

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
/**
4
 * RoboFile.php
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-cli-simple
18
 * @link      http://www.techdivision.com
19
 */
20
21
use Lurker\Event\FilesystemEvent;
22
23
use Symfony\Component\Finder\Finder;
24
25
/**
26
 * Defines the available build tasks.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import-cli-simple
32
 * @link      http://www.techdivision.com
33
 */
34
class RoboFile extends \Robo\Tasks
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
35
{
36
37
    /**
38
     * The build properties.
39
     *
40
     * @var array
41
     */
42
    protected $properties = array(
43
        'base.dir' => __DIR__,
44
        'etc.dir' => __DIR__ . '/etc',
45
        'src.dir' => __DIR__ . '/src',
46
        'dist.dir' => __DIR__ . '/dist',
47
        'vendor.dir' => __DIR__ . '/vendor',
48
        'target.dir' => __DIR__ . '/target',
49
        'webapp.name' => 'import-cli-simple',
50
        'webapp.version' => '1.0.0-alpha62'
51
    );
52
53
    /**
54
     * Run's the composer install command.
55
     *
56
     * @return void
57
     */
58
    public function composerInstall()
59
    {
60
        // optimize autoloader with custom path
61
        $this->taskComposerInstall()
62
             ->preferDist()
63
             ->optimizeAutoloader()
64
             ->run();
65
    }
66
67
    /**
68
     * Run's the composer update command.
69
     *
70
     * @return void
71
     */
72
    public function composerUpdate()
73
    {
74
        // optimize autoloader with custom path
75
        $this->taskComposerUpdate()
76
             ->preferDist()
77
             ->optimizeAutoloader()
78
             ->run();
79
    }
80
81
    /**
82
     * Clean up the environment for a new build.
83
     *
84
     * @return void
85
     */
86
    public function clean()
87
    {
88
        $this->taskDeleteDir($this->properties['target.dir'])->run();
89
    }
90
91
    /**
92
     * Prepare's the environment for a new build.
93
     *
94
     * @return void
95
     */
96
    public function prepare()
97
    {
98
        $this->taskFileSystemStack()
99
             ->mkdir($this->properties['dist.dir'])
100
             ->mkdir($this->properties['target.dir'])
101
             ->mkdir(sprintf('%s/reports', $this->properties['target.dir']))
102
             ->run();
103
    }
104
105
    /**
106
     * Creates the a PHAR archive from the sources.
107
     *
108
     * @return void
109
     */
110
    public function createPhar()
111
    {
112
113
        // prepare the PHAR archive name
114
        $archiveName = sprintf(
115
            '%s/%s.phar',
116
            $this->properties['target.dir'],
117
            $this->properties['webapp.name']
118
        );
119
120
        // prepare the target directory
121
        $targetDir = $this->properties['target.dir'] . DIRECTORY_SEPARATOR . $this->properties['webapp.version'];
122
123
        // copy the composer.json file
124
        $this->taskFilesystemStack()
125
             ->copy(
126
                  __DIR__ . DIRECTORY_SEPARATOR . 'composer.json',
127
                  $targetDir. DIRECTORY_SEPARATOR. 'composer.json'
128
             )->run();
129
130
          // copy the composer.json file
131
          $this->taskFilesystemStack()
132
               ->copy(
133
                   __DIR__ . DIRECTORY_SEPARATOR . '.semver',
134
                   $targetDir. DIRECTORY_SEPARATOR. '.semver'
135
               )->run();
136
137
        // copy the src/etc directory
138
        $this->taskCopyDir(
139
                  array(
140
                      $this->properties['src.dir'] => $targetDir . DIRECTORY_SEPARATOR . 'src'
141
                  )
142
               )->run();
143
144
        // install the composer dependencies
145
        $this->taskComposerInstall()
146
            ->dir($targetDir)
147
            ->noDev()
148
            ->optimizeAutoloader()
149
            ->run();
150
151
        // prepare the task
152
        $pharTask = $this->taskPackPhar($archiveName)
153
            ->compress()
154
            ->stub('stub.php');
155
156
        // load a list with all the source files
157
        $finder = Finder::create()
158
            ->name('*.php')
159
            ->in($targetDir . DIRECTORY_SEPARATOR . 'src');
160
161
        // iterate over the source files and add them to the PHAR archive
162
        foreach ($finder as $file) {
163
            $pharTask->addFile('src/' . $file->getRelativePathname(), $file->getRealPath());
164
        }
165
166
        // load a list with all the source files from the vendor directory
167
        $finder = Finder::create()->files()
168
            ->name('*.php')
169
            ->name('services.xml')
170
            ->name('services-1.0.xsd')
171
            ->name('techdivision-import.json')
172
            ->in($targetDir . DIRECTORY_SEPARATOR . 'vendor');
173
174
        // iterate over the source files of the vendor directory and add them to the PHAR archive
175
        foreach ($finder as $file) {
176
            $pharTask->addFile('vendor/' . $file->getRelativePathname(), $file->getRealPath());
177
        }
178
179
        // add the semver file and create the PHAR archive
180
        $pharTask->addFile('.semver', realpath($targetDir. DIRECTORY_SEPARATOR. '.semver'));
181
        $pharTask->run();
182
183
        // verify PHAR archive is packed correctly
184
        $this->_exec(sprintf('php %s', $archiveName));
185
186
        // prepare the PHAR archive distribution name
187
        $distArchiveName = sprintf('%s/%s.phar', $this->properties['dist.dir'], $this->properties['webapp.name']);
188
189
        // clean up the dist directory
190
        $this->taskCleanDir($this->properties['dist.dir'])->run();
191
192
        // copy the latest PHAR archive to the dist directory
193
        $this->taskFilesystemStack()->copy($archiveName, $distArchiveName)->run();
194
    }
195
196
    /**
197
     * Run's the PHPMD.
198
     *
199
     * @return void
200
     */
201 View Code Duplication
    public function runMd()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
    {
203
204
        // run the mess detector
205
        $this->_exec(
206
            sprintf(
207
                '%s/bin/phpmd %s xml phpmd.xml --reportfile %s/reports/pmd.xml --ignore-violations-on-exit',
208
                $this->properties['vendor.dir'],
209
                $this->properties['src.dir'],
210
                $this->properties['target.dir']
211
            )
212
        );
213
    }
214
215
    /**
216
     * Run's the PHPCPD.
217
     *
218
     * @return void
219
     */
220 View Code Duplication
    public function runCpd()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
221
    {
222
223
        // run the copy past detector
224
        $this->_exec(
225
            sprintf(
226
                '%s/bin/phpcpd %s --log-pmd %s/reports/pmd-cpd.xml --names-exclude *Factory.php',
227
                $this->properties['vendor.dir'],
228
                $this->properties['src.dir'],
229
                $this->properties['target.dir']
230
            )
231
        );
232
    }
233
234
    /**
235
     * Run's the PHPCodeSniffer.
236
     *
237
     * @return void
238
     */
239 View Code Duplication
    public function runCs()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
240
    {
241
242
        // run the code sniffer
243
        $this->_exec(
244
            sprintf(
245
                '%s/bin/phpcs -n --report-full --extensions=php --standard=phpcs.xml --report-checkstyle=%s/reports/phpcs.xml %s',
246
                $this->properties['vendor.dir'],
247
                $this->properties['target.dir'],
248
                $this->properties['src.dir']
249
            )
250
        );
251
    }
252
253
    /**
254
     * Run's the PHPUnit tests.
255
     *
256
     * @return void
257
     */
258
    public function runTests()
259
    {
260
261
        // run PHPUnit
262
        $this->taskPHPUnit(sprintf('%s/bin/phpunit', $this->properties['vendor.dir']))
263
             ->configFile('phpunit.xml')
264
             ->run();
265
    }
266
267
    /**
268
     * Raising the semver version number.
269
     *
270
     * @return void
271
     */
272
    public function semver()
273
    {
274
        $this->taskSemVer('.semver')
275
             ->prerelease('alpha')
276
             ->run();
277
    }
278
279
    /**
280
     * The complete build process.
281
     *
282
     * @return void
283
     */
284
    public function build()
285
    {
286
        $this->clean();
287
        $this->prepare();
288
        $this->runCs();
289
        $this->runCpd();
290
        $this->runMd();
291
        $this->runTests();
292
    }
293
}
294