Passed
Push — master ( b8148a...2f69bc )
by Michiel
06:00
created

AdhocTaskdefTask::filterValidTasks()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 1
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 10
c 1
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
 * A class for creating adhoc tasks in build file.
25
 *
26
 * <target name="test-adhoc">
27
 *        <adhoc-task name="foo"><![CDATA[
28
 *
29
 *            class FooTest extends Task {
30
 *                private $bar;
31
 *
32
 *                function setBar($bar) {
33
 *                    $this->bar = $bar;
34
 *                }
35
 *
36
 *                function main() {
37
 *                    $this->log("In FooTest: " . $this->bar);
38
 *                }
39
 *            }
40
 *
41
 *        ]]></adhoc-task>
42
 *
43
 *      <foo bar="B.L.I.N.G"/>
44
 * </target>
45
 *
46
 * @author  Hans Lellelid <[email protected]>
47
 * @package phing.tasks.system
48
 */
49
class AdhocTaskdefTask extends AdhocTask
50
{
51
52
    /**
53
     * The tag that refers to this task.
54
     */
55
    private $name;
56
57
    /**
58
     * Set the tag that will represent this adhoc task/type.
59
     *
60
     * @param string $name
61
     */
62 4
    public function setName($name)
63
    {
64 4
        $this->name = $name;
65 4
    }
66
67
    /**
68
     * Main entry point
69
     */
70 4
    public function main()
71
    {
72 4
        if ($this->name === null) {
73
            throw new BuildException("The name attribute is required for adhoc task definition.", $this->getLocation());
74
        }
75
76 4
        $taskdefs = $this->getProject()->getTaskDefinitions();
77
78 4
        if (!isset($taskdefs[$this->name])) {
79 4
            $this->execute();
80
81 4
            $classes = $this->filterValidTasks($this->getNewClasses());
82
83 4
            if (count($classes) < 1) {
84
                throw new BuildException(
85
                    "You must define at least one class that is a sub-class of " . Task::class,
86
                    $this->getLocation()
87
                );
88
            }
89
90 4
            $classname = array_shift($classes);
91
92 4
            $this->log("Task " . $this->name . " will be handled by class " . $classname, Project::MSG_VERBOSE);
93 4
            $this->project->addTaskDefinition($this->name, $classname);
94
        }
95 4
    }
96
97
    /**
98
     * @param string[] $classes
99
     * @return string[]
100
     * @throws ReflectionException
101
     */
102 4
    private function filterValidTasks(array $classes): array
103
    {
104 4
        return array_filter($classes, function ($classname) {
105 4
            $t = new ReflectionClass($classname);
106
107 4
            if (!$t->isSubclassOf(Task::class) || !$t->isInstantiable()) {
108 1
                return false;
109
            }
110
111 4
            return true;
112 4
        });
113
    }
114
}
115