Completed
Pull Request — master (#74)
by Tim
14:27
created

RoboFile   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 272
Duplicated Lines 14.34 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 11
dl 39
loc 272
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A composerInstall() 0 8 1
A composerUpdate() 0 8 1
A clean() 0 4 1
A prepare() 0 8 1
B createPhar() 0 96 4
A runMd() 13 13 1
A runCpd() 13 13 1
A runCs() 13 13 1
A semver() 0 6 1
A build() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
                      $this->properties['etc.dir'] => $targetDir . DIRECTORY_SEPARATOR . 'etc'
142
                  )
143
               )->run();
144
145
        // install the composer dependencies
146
        $this->taskComposerInstall()
147
            ->dir($targetDir)
148
            ->noDev()
149
            ->optimizeAutoloader()
150
            ->run();
151
152
        // prepare the task
153
        $pharTask = $this->taskPackPhar($archiveName)
154
            ->compress()
155
            ->stub('stub.php');
156
157
        // load a list with all the source files
158
        $finder = Finder::create()
159
            ->name('*.php')
160
            ->in($targetDir . DIRECTORY_SEPARATOR . 'src');
161
162
        // iterate over the source files and add them to the PHAR archive
163
        foreach ($finder as $file) {
164
            $pharTask->addFile('src/' . $file->getRelativePathname(), $file->getRealPath());
165
        }
166
167
        // load a list with all the source files from the vendor directory
168
        $finder = Finder::create()->files()
169
            ->name('*.php')
170
            ->name('services.xml')
171
            ->name('services-1.0.xsd')
172
            ->name('techdivision-import.json')
173
            ->in($targetDir . DIRECTORY_SEPARATOR . 'vendor');
174
175
        // iterate over the source files of the vendor directory and add them to the PHAR archive
176
        foreach ($finder as $file) {
177
            $pharTask->addFile('vendor/' . $file->getRelativePathname(), $file->getRealPath());
178
        }
179
180
        // load a list with all the DI configuration files from the etc directory
181
        $finder = Finder::create()->files()
182
            ->name('*.xml')
183
            ->in($targetDir . DIRECTORY_SEPARATOR . 'etc');
184
185
        // iterate over the DI configuration files of the etc directory and add them to the PHAR archive
186
        foreach ($finder as $file) {
187
            $pharTask->addFile('etc/' . $file->getRelativePathname(), $file->getRealPath());
188
        }
189
190
        // add the semver file and create the PHAR archive
191
        $pharTask->addFile('.semver', realpath($targetDir. DIRECTORY_SEPARATOR. '.semver'));
192
        $pharTask->run();
193
194
        // verify PHAR archive is packed correctly
195
        $this->_exec(sprintf('php %s', $archiveName));
196
197
        // prepare the PHAR archive distribution name
198
        $distArchiveName = sprintf('%s/%s.phar', $this->properties['dist.dir'], $this->properties['webapp.name']);
199
200
        // clean up the dist directory
201
        $this->taskCleanDir($this->properties['dist.dir'])->run();
202
203
        // copy the latest PHAR archive to the dist directory
204
        $this->taskFilesystemStack()->copy($archiveName, $distArchiveName)->run();
205
    }
206
207
    /**
208
     * Run's the PHPMD.
209
     *
210
     * @return void
211
     */
212 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...
213
    {
214
215
        // run the mess detector
216
        $this->_exec(
217
            sprintf(
218
                '%s/bin/phpmd %s xml phpmd.xml --reportfile %s/reports/pmd.xml --ignore-violations-on-exit',
219
                $this->properties['vendor.dir'],
220
                $this->properties['src.dir'],
221
                $this->properties['target.dir']
222
            )
223
        );
224
    }
225
226
    /**
227
     * Run's the PHPCPD.
228
     *
229
     * @return void
230
     */
231 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...
232
    {
233
234
        // run the copy past detector
235
        $this->_exec(
236
            sprintf(
237
                '%s/bin/phpcpd %s --log-pmd %s/reports/pmd-cpd.xml --names-exclude *Factory.php',
238
                $this->properties['vendor.dir'],
239
                $this->properties['src.dir'],
240
                $this->properties['target.dir']
241
            )
242
        );
243
    }
244
245
    /**
246
     * Run's the PHPCodeSniffer.
247
     *
248
     * @return void
249
     */
250 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...
251
    {
252
253
        // run the code sniffer
254
        $this->_exec(
255
            sprintf(
256
                '%s/bin/phpcs -n --report-full --extensions=php --standard=phpcs.xml --report-checkstyle=%s/reports/phpcs.xml %s',
257
                $this->properties['vendor.dir'],
258
                $this->properties['target.dir'],
259
                $this->properties['src.dir']
260
            )
261
        );
262
    }
263
264
    /**
265
     * Run's the PHPUnit tests.
266
     *
267
     * @return void
268
     */
269
    public function runTests()
270
    {
271
272
        // run PHPUnit
273
        $this->taskPHPUnit(sprintf('%s/bin/phpunit', $this->properties['vendor.dir']))
274
             ->configFile('phpunit.xml')
275
             ->run();
276
    }
277
278
    /**
279
     * Raising the semver version number.
280
     *
281
     * @return void
282
     */
283
    public function semver()
284
    {
285
        $this->taskSemVer('.semver')
286
             ->prerelease('alpha')
287
             ->run();
288
    }
289
290
    /**
291
     * The complete build process.
292
     *
293
     * @return void
294
     */
295
    public function build()
296
    {
297
        $this->clean();
298
        $this->prepare();
299
        $this->runCs();
300
        $this->runCpd();
301
        $this->runMd();
302
        $this->runTests();
303
        $this->createPhar();
304
    }
305
}
306