Passed
Push — master ( b5ca42...8fe651 )
by Siad
05:05
created

AdhocTask   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 56.25%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 58
ccs 9
cts 16
cp 0.5625
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 5 1
A addText() 0 3 1
A main() 0 9 3
A getNewClasses() 0 3 1
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
use Phing\Exception\BuildException;
21
use Phing\Project;
22
use Phing\Task;
23
24
/**
25
 * Abstract class for creating adhoc Phing components in buildfile.
26
 *
27
 * By itself this class can be used to declare a single class within your buildfile.
28
 * You can then reference this class in any task that takes custom classes (selectors,
29
 * mappers, filters, etc.)
30
 *
31
 * Subclasses exist for conveniently declaring and registering tasks and types.
32
 *
33
 * @author  Hans Lellelid <[email protected]>
34
 * @package phing.tasks.system
35
 */
36
class AdhocTask extends Task
37
{
38
39
    /**
40
     * The PHP script
41
     *
42
     * @var string
43
     */
44
    protected $script;
45
46
    protected $newClasses = [];
47
48
    /**
49
     * Main entry point
50
     */
51
    public function main()
52
    {
53
        $this->execute();
54
        if ($this->newClasses) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->newClasses of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
55
            foreach ($this->newClasses as $classname) {
56
                $this->log("Added adhoc class " . $classname, Project::MSG_VERBOSE);
57
            }
58
        } else {
59
            $this->log("Adhoc task executed but did not result in any new classes.", Project::MSG_VERBOSE);
60
        }
61
    }
62
63
    /**
64
     * Get array of names of newly defined classes.
65
     *
66
     * @return array
67
     */
68 4
    protected function getNewClasses()
69
    {
70 4
        return $this->newClasses;
71
    }
72
73
    /**
74
     * Load the adhoc class, and perform any core validation.
75
     *
76
     * @return string         The classname of the ProjectComponent class.
77
     * @throws BuildException - if more than one class is defined.
78
     */
79 4
    protected function execute()
80
    {
81 4
        $classes = get_declared_classes();
82 4
        eval($this->script);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
83 4
        $this->newClasses = array_diff(get_declared_classes(), $classes);
84 4
    }
85
86
    /**
87
     * Set the script.
88
     *
89
     * @param string $script
90
     */
91 4
    public function addText($script)
92
    {
93 4
        $this->script = $script;
94 4
    }
95
}
96