PharTask::getStub()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Conveyor package.
5
 *
6
 * (c) Jeroen Fiege <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webcreate\Conveyor\Task;
13
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\Finder\SplFileInfo;
16
use Webcreate\Conveyor\IO\IOInterface;
17
use Webcreate\Conveyor\Repository\Version;
18
use Webcreate\Conveyor\Task\Result\ExecuteResult;
19
20
/**
21
 * @todo check requirement: phar.readonly = Off in php.ini
22
 * @author jeroen
23
 *
24
 */
25
class PharTask extends Task
26
{
27
    /**
28
     * @var string
29
     */
30
    protected $buildDir;
31
32
    /**
33
     * @var \Webcreate\Conveyor\IO\IOInterface
34
     */
35
    protected $io;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
36
    protected $conveyorFile;
37
    protected $filename;
38
    protected $stub;
39
40
    /**
41
     * Constructor.
42
     *
43
     * @param string $buildDir
44
     * @param $conveyorFile
45
     * @param IOInterface $io
46
     */
47
    public function __construct($buildDir, $conveyorFile, IOInterface $io = null)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
48
    {
49
        $this->io           = $io;
50
        $this->buildDir     = $buildDir;
51
        $this->conveyorFile = $conveyorFile;
52
    }
53
54
    /**
55
     * @todo implement an excluded file list, instead of hard-coding the filter for the conveyorFile
56
     *
57
     * @inheritdoc
58
     */
59
    public function execute($target, Version $version = null)
60
    {
61
        $filename = $this->options['filename'];
62
        $conveyorFile = $this->conveyorFile;
0 ignored issues
show
Unused Code introduced by
$conveyorFile is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
64
        $this->output(sprintf('Creating Phar...'));
65
66
        $phar = new \Phar($this->buildDir . '/' . $filename, 0, $filename);
67
        $phar->setSignatureAlgorithm(\Phar::SHA1);
68
69
        $phar->startBuffering();
70
71
        $finder = new Finder();
72
        $finder
73
            ->files()
74
            ->ignoreVCS(true)
75
            ->in($this->buildDir)
76
        ;
77
78
        foreach ($finder as $file) {
79
            $this->addFile($phar, $file, false);
0 ignored issues
show
Unused Code introduced by
The call to PharTask::addFile() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
80
        }
81
82
        // Stubs
83
        $phar->setStub($this->getStub());
84
85
        $phar->stopBuffering();
86
87
        unset($phar);
88
89
        return new ExecuteResult(array($filename));
90
    }
91
92
    /**
93
     * @param \Phar $phar
94
     * @param SplFileInfo $file
95
     */
96
    private function addFile(\Phar $phar, SplFileInfo $file)
97
    {
98
        $path = $file->getRelativePathname();
99
100
        $content = file_get_contents($file);
101
102
        $content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content);
103
104
        $phar->addFromString($path, $content);
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    private function getStub()
111
    {
112
        $stub = <<<'EOF'
113
#!/usr/bin/env php
114
<?php
115
116
Phar::mapPhar('@@filename@@');
117
118
require 'phar://@@filename@@/@@stub@@';
119
120
__HALT_COMPILER();
121
122
EOF;
123
124
        $stub = str_replace('@@filename@@', $this->options['filename'], $stub);
125
        $stub = str_replace('@@stub@@', $this->options['stub'], $stub);
126
127
        return $stub;
128
    }
129
}
130