Packager   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 123
ccs 0
cts 64
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B package() 0 59 5
1
<?php
2
/**
3
 * For licensing information, please see the LICENSE file accompanied with this file.
4
 *
5
 * @author Gerard van Helden <[email protected]>
6
 * @copyright 2012 Gerard van Helden <http://melp.nl>
7
 */
8
9
namespace Zicht\Tool\Packager;
10
11
use Symfony\Component\Finder\Finder;
12
use Zicht\Tool\Script\Buffer;
13
14
/**
15
 * PHAR Packager tool
16
 */
17
class Packager
18
{
19
    /**
20
     * Packager to generate an executable PHAR for Z or a derivative tool.
21
     *
22
     * @param string $root
23
     * @param array $options
24
     */
25
    public function __construct($root, array $options)
26
    {
27
        $this->srcRoot = $root;
0 ignored issues
show
Bug introduced by
The property srcRoot does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
        $this->options = $options + array(
0 ignored issues
show
Bug introduced by
The property options does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
            'config-filename'   => 'z.yml',
30
            'app-name'          => 'The Zicht Tool',
31
            'app-version'       => 'development build (' . date('r') . ')',
32
            'static'            => false,
33
            'static-plugin-paths' => array(getcwd(), $root . '/vendor/zicht/z-plugins')
34
        );
35
    }
36
37
38
    /**
39
     * Build the package file.
40
     * Throws an exception if the file already exists. Pass $force as true to override this.
41
     *
42
     * @param string $targetFile
43
     * @param bool $force
44
     * @return string
45
     *
46
     * @throws \RuntimeException
47
     */
48
    public function package($targetFile, $force)
49
    {
50
        if (is_file($targetFile)) {
51
            if ($force) {
52
                unlink($targetFile);
53
            } else {
54
                throw new \RuntimeException("File {$targetFile} already exists");
55
            }
56
        }
57
        $curDir = getcwd();
58
        $buildFile = 'build.phar';
59
        $phar = new \Phar($buildFile);
60
61
        if ($static = $this->options['static']) {
0 ignored issues
show
Unused Code introduced by
$static 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...
62
            $stub = new Node\StaticStub(
63
                $phar,
64
                $this->options['app-name'],
65
                $this->options['app-version'],
66
                $this->options['static'],
67
                $this->options['static-plugin-paths']
68
            );
69
        } else {
70
            $stub = new Node\DynamicStub(
71
                $phar,
72
                $this->options['app-name'],
73
                $this->options['app-version'],
74
                $this->options['config-filename']
75
            );
76
        }
77
        $buffer = new Buffer();
78
        $buffer
79
            ->writeln('#!/usr/bin/env php')
80
            ->writeln('<?php')
81
            ->writeln(self::$HEADER)
82
        ;
83
        $stub->compile($buffer);
84
85
        chdir($this->srcRoot);
86
        $finder = new Finder();
87
        $files = $finder
88
            ->in(array('vendor', 'src'))
89
            ->ignoreVCS(true)
90
            // Finder is only used by the packager, not by Z itself.
91
            ->exclude(array('vendor/symfony/finder'))
92
            ->files();
93
94
        foreach ($files as $file) {
95
            $phar[$file->getPathname()] = file_get_contents($file->getPathname());
96
        }
97
        $phar['LICENSE'] = file_get_contents('LICENSE');
98
        chdir($curDir);
99
100
        $phar->setStub($buffer->getResult());
101
        rename($buildFile, $targetFile);
102
        chmod($targetFile, 0755);
103
        chdir($curDir);
104
105
        return realpath($targetFile);
106
    }
107
108
    private static $HEADER = <<<EOHEADER
109
/**
110
 * This file was built with the Z packager. For more information,
111
 * visit the Z website at http://z.zicht.nl/, or contact [email protected]
112
 *
113
 * Please pay your respects by leaving these notices in tact.
114
 */
115
/**
116
 * Copyright (C) 2013 Zicht online, Gerard van Helden
117
 *
118
 * Permission is hereby granted, free of charge, to any person
119
 * obtaining a copy of this software and associated documentation
120
 * files (the "Software"), to deal in the Software without restriction,
121
 * including without limitation the rights to use, copy, modify, merge,
122
 * publish, distribute, sublicense, and/or sell copies of the Software,
123
 * and to permit persons to whom the Software is furnished to do so,
124
 * subject to the following conditions:
125
 *
126
 * The above copyright notice and this permission notice shall be
127
 * included in all copies or substantial portions of the Software.
128
 *
129
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
130
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
131
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
132
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
133
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
134
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
135
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
136
 * SOFTWARE.
137
 */
138
EOHEADER;
139
}
140