Passed
Push — master ( eabf33...342b63 )
by Michiel
05:53
created

AdhocTask::getNewClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
23
/**
24
 * Abstract class for creating adhoc Phing components in buildfile.
25
 *
26
 * By itself this class can be used to declare a single class within your buildfile.
27
 * You can then reference this class in any task that takes custom classes (selectors,
28
 * mappers, filters, etc.)
29
 *
30
 * Subclasses exist for conveniently declaring and registering tasks and types.
31
 *
32
 * @author  Hans Lellelid <[email protected]>
33
 * @package phing.tasks.system
34
 */
35
class AdhocTask extends Task
36
{
37
38
    /**
39
     * The PHP script
40
     *
41
     * @var string
42
     */
43
    protected $script;
44
45
    protected $newClasses = [];
46
47
    /**
48
     * Main entry point
49
     */
50
    public function main()
51
    {
52
        $this->execute();
53
        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...
54
            foreach ($this->newClasses as $classname) {
55
                $this->log("Added adhoc class " . $classname, Project::MSG_VERBOSE);
56
            }
57
        } else {
58
            $this->log("Adhoc task executed but did not result in any new classes.", Project::MSG_VERBOSE);
59
        }
60
    }
61
62
    /**
63
     * Get array of names of newly defined classes.
64
     *
65
     * @return array
66
     */
67 4
    protected function getNewClasses()
68
    {
69 4
        return $this->newClasses;
70
    }
71
72
    /**
73
     * Load the adhoc class, and perform any core validation.
74
     *
75
     * @return string         The classname of the ProjectComponent class.
76
     * @throws BuildException - if more than one class is defined.
77
     */
78 4
    protected function execute()
79
    {
80 4
        $classes = get_declared_classes();
81 4
        eval($this->script);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
82 4
        $this->newClasses = array_diff(get_declared_classes(), $classes);
83 4
    }
84
85
    /**
86
     * Set the script.
87
     *
88
     * @param string $script
89
     */
90 4
    public function addText($script)
91
    {
92 4
        $this->script = $script;
93 4
    }
94
}
95