Passed
Push — master ( e1f86a...4e1a3a )
by Siad
05:23
created

PhingTask::processFile()   F

Complexity

Conditions 29
Paths > 20000

Size

Total Lines 141
Code Lines 86

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 71
CRAP Score 30.5841

Importance

Changes 0
Metric Value
cc 29
eloc 86
nc 66176
nop 0
dl 0
loc 141
ccs 71
cts 81
cp 0.8765
crap 30.5841
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
namespace Phing\Tasks\System;
21
22
use Phing\Exception\BuildException;
23
use Phing\Io\FileOutputStream;
24
use Phing\Io\FileUtils;
25
use Phing\Io\OutputStream;
26
use Phing\Io\File;
27
use Phing\Listener\DefaultLogger;
28
use Phing\Parser\ProjectConfigurator;
29
use Phing\Phing;
30
use Phing\Project;
31
use Phing\ProjectComponent;
32
use Phing\Target;
33
use Phing\Task;
34
use Phing\Type\Element\FileSetAware;
35
use Phing\Type\Reference;
36
37
/**
38
 * Task that invokes phing on another build file.
39
 *
40
 * Use this task, for example, if you have nested buildfiles in your project. Unlike
41
 * AntTask, PhingTask can even support filesets:
42
 *
43
 * <pre>
44
 *   <phing>
45
 *    <fileset dir="${srcdir}">
46
 *      <include name="** /build.xml" /> <!-- space added after ** is there because of PHP comment syntax -->
47
 *      <exclude name="build.xml" />
48
 *    </fileset>
49
 *   </phing>
50
 * </pre>
51
 *
52
 * @author  Hans Lellelid <[email protected]>
53
 * @package phing.tasks.system
54
 */
55
class PhingTask extends Task
56
{
57
    use FileSetAware;
58
59
    /**
60
     * the basedir where is executed the build file
61
     * @var File
62
     */
63
    private $dir;
64
65
    /**
66
     * build.xml (can be absolute) in this case dir will be ignored
67
     */
68
    private $phingFile;
69
70
    /**
71
     * the target to call if any
72
     * @var Target
73
     */
74
    protected $newTarget;
75
76
    /**
77
     * should we inherit properties from the parent ?
78
     */
79
    private $inheritAll = true;
80
81
    /**
82
     * should we inherit references from the parent ?
83
     */
84
    private $inheritRefs = false;
85
86
    /**
87
     * the properties to pass to the new project
88
     */
89
    private $properties = [];
90
91
    /**
92
     * the references to pass to the new project
93
     */
94
    private $references = [];
95
96
    /**
97
     * The temporary project created to run the build file
98
     *
99
     * @var Project
100
     */
101
    private $newProject;
102
103
    /**
104
     * Fail the build process when the called build fails?
105
     */
106
    private $haltOnFailure = false;
107
108
    /**
109
     * Whether the basedir of the new project should be the same one
110
     * as it would be when running the build file directly -
111
     * independent of dir and/or inheritAll settings.
112
     */
113
    private $useNativeBasedir = false;
114
115
    /**
116
     * @var OutputStream
117
     */
118
    private $out;
119
120
    /** @var string */
121
    private $output;
122
123
    /**
124
     * @var array
125
     */
126
    private $locals;
127
128 36
    public function __construct(Task $owner = null)
129
    {
130 36
        if ($owner !== null) {
131 13
            $this->bindToOwner($owner);
132
        }
133 36
        parent::__construct();
134 36
    }
135
136
    /**
137
     *  If true, abort the build process if there is a problem with or in the target build file.
138
     *  Defaults to false.
139
     *
140
     * @param bool $hof new value
141
     */
142 17
    public function setHaltOnFailure($hof)
143
    {
144 17
        $this->haltOnFailure = (bool) $hof;
145 17
    }
146
147
    /**
148
     * Whether the basedir of the new project should be the same one
149
     * as it would be when running the build file directly -
150
     * independent of dir and/or inheritAll settings.
151
     *
152
     * @param bool $b
153
     */
154
    public function setUseNativeBasedir(bool $b)
155
    {
156
        $this->useNativeBasedir = $b;
157
    }
158
159
    /**
160
     * Creates a Project instance for the project to call.
161
     *
162
     * @return void
163
     */
164 36
    public function init()
165
    {
166 36
        $this->newProject = $this->getProject()->createSubProject();
167 36
        $tdf = $this->project->getTaskDefinitions();
168 36
        $this->newProject->addTaskDefinition("property", $tdf["property"]);
169 36
    }
170
171
    /**
172
     * Called in execute or createProperty if newProject is null.
173
     *
174
     * <p>This can happen if the same instance of this task is run
175
     * twice as newProject is set to null at the end of execute (to
176
     * save memory and help the GC).</p>
177
     *
178
     * <p>Sets all properties that have been defined as nested
179
     * property elements.</p>
180
     */
181 6
    private function reinit()
182
    {
183 6
        $this->init();
184
185 6
        $count = count($this->properties);
186 6
        for ($i = 0; $i < $count; $i++) {
187
            /**
188
             * @var PropertyTask $p
189
             */
190 6
            $p = $this->properties[$i] ?? null;
191 6
            if ($p !== null) {
192
                /** @var PropertyTask $newP */
193 6
                $newP = $this->newProject->createTask("property");
194 6
                $newP->setName($p->getName());
195 6
                if ($p->getValue() !== null) {
196 6
                    $newP->setValue($p->getValue());
197
                }
198 6
                if ($p->getFile() !== null) {
199
                    $newP->setFile($p->getFile());
200
                }
201 6
                if ($p->getPrefix() !== null) {
202
                    $newP->setPrefix($p->getPrefix());
203
                }
204 6
                if ($p->getRefid() !== null) {
205
                    $newP->setRefid($p->getRefid());
206
                }
207 6
                if ($p->getEnvironment() !== null) {
208
                    $newP->setEnvironment($p->getEnvironment());
209
                }
210 6
                if ($p->getUserProperty() !== null) {
211 6
                    $newP->setUserProperty($p->getUserProperty());
212
                }
213 6
                $newP->setOverride($p->getOverride());
214 6
                $newP->setLogoutput($p->getLogoutput());
215 6
                $newP->setQuiet($p->getQuiet());
216
217 6
                $this->properties[$i] = $newP;
218
            }
219
        }
220 6
    }
221
222
    /**
223
     * Main entry point for the task.
224
     *
225
     * @return void
226
     */
227 33
    public function main()
228
    {
229
        // Call Phing on the file set with the attribute "phingfile"
230 33
        if ($this->phingFile !== null || $this->dir !== null) {
231 33
            $this->processFile();
232
        }
233
234
        // if no filesets are given stop here; else process filesets
235 27
        if (!empty($this->filesets)) {
236
            // preserve old settings
237
            $savedDir = $this->dir;
238
            $savedPhingFile = $this->phingFile;
239
            $savedTarget = $this->newTarget;
240
241
            // set no specific target for files in filesets
242
            // [HL] I'm commenting this out; I don't know why this should not be supported!
243
            // $this->newTarget = null;
244
245
            foreach ($this->filesets as $fs) {
246
                $ds = $fs->getDirectoryScanner($this->project);
247
248
                $fromDir = $fs->getDir($this->project);
0 ignored issues
show
Unused Code introduced by
The assignment to $fromDir is dead and can be removed.
Loading history...
249
                $srcFiles = $ds->getIncludedFiles();
250
251
                foreach ($srcFiles as $fname) {
252
                    $f = new File($ds->getbasedir(), $fname);
253
                    $f = $f->getAbsoluteFile();
254
                    $this->phingFile = $f->getAbsolutePath();
255
                    $this->dir = $f->getParentFile();
256
                    $this->processFile(); // run Phing!
257
                }
258
            }
259
260
            // side effect free programming ;-)
261
            $this->dir = $savedDir;
262
            $this->phingFile = $savedPhingFile;
263
            $this->newTarget = $savedTarget;
264
265
            // [HL] change back to correct dir
266
            if ($this->dir !== null) {
267
                chdir($this->dir->getAbsolutePath());
268
            }
269
        }
270
271
        // Remove any dangling references to help the GC
272 27
        foreach ($this->properties as $property) {
273 12
            $property->setFallback(null);
274
        }
275 27
    }
276
277
    /**
278
     * Execute phing file.
279
     *
280
     * @throws BuildException
281
     */
282 33
    private function processFile(): void
283
    {
284 33
        $buildFailed = false;
285 33
        $savedDir = $this->dir;
286 33
        $savedPhingFile = $this->phingFile;
287 33
        $savedTarget = $this->newTarget;
288
289 33
        $savedBasedirAbsPath = null; // this is used to save the basedir *if* we change it
290
291
        try {
292 33
            $this->getNewProject();
293
294 33
            $this->initializeProject();
295
296 33
            if ($this->dir !== null) {
297 12
                if (!$this->useNativeBasedir) {
298 12
                    $this->newProject->setBasedir($this->dir);
299 12
                    if ($savedDir !== null) { // has been set explicitly
300 12
                        $this->newProject->setInheritedProperty('project.basedir', $this->dir->getAbsolutePath());
301
                    }
302
                }
303
            } else {
304
                // Since we're not changing the basedir here (for file resolution),
305
                // we don't need to worry about any side-effects in this scanrio.
306 22
                $this->dir = $this->getProject()->getBasedir();
307
            }
308
309 33
            $this->overrideProperties();
310 33
            $this->phingFile = $this->phingFile ?? 'build.xml';
311
312 33
            $fu = new FileUtils();
313 33
            $file = $fu->resolveFile($this->dir, $this->phingFile);
314 33
            $this->phingFile = $file->getAbsolutePath();
315 33
            $this->log('calling target(s) '
316 33
                . (empty($this->locals) ? '[default]' : implode(', ', $this->locals))
317 33
                . ' in build file ' . $this->phingFile, Project::MSG_VERBOSE);
318
319 33
            $this->newProject->setUserProperty("phing.file", $this->phingFile);
320
321 33
            if (empty($this->locals)) {
322 33
                $defaultTarget = $this->newProject->getDefaultTarget();
323 33
                if (!empty($defaultTarget)) {
324 33
                    $this->locals[] = $defaultTarget;
325
                }
326
            }
327
328 33
            $thisPhingFile = $this->getProject()->getProperty('phing.file');
329
            // Are we trying to call the target in which we are defined (or
330
            // the build file if this is a top level task)?
331
            if (
332 33
                $thisPhingFile !== null
333 33
                && $this->getOwningTarget() !== null
334 33
                && $thisPhingFile === $file->getPath()
335 33
                && $this->getOwningTarget()->getName() === ''
336
            ) {
337
                if ('phingcall' === $this->getTaskName()) {
338
                    throw new BuildException('phingcall must not be used at the top level.');
339
                }
340
                throw new BuildException(
341
                    sprintf(
342
                        '%s task at the top level must not invoke its own build file.',
343
                        $this->getTaskName()
344
                    )
345
                );
346
            }
347
348 33
            ProjectConfigurator::configureProject($this->newProject, new File($this->phingFile));
349
350 30
            if ($this->newTarget === null) {
351 3
                $this->newTarget = $this->newProject->getDefaultTarget();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->newProject->getDefaultTarget() of type string is incompatible with the declared type Phing\Target of property $newTarget.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
352
            }
353
354
            // Are we trying to call the target in which we are defined?
355
            if (
356 30
                $this->newProject->getBasedir()->equals($this->project->getBasedir())
357 30
                && $this->newProject->getProperty('phing.file') === $this->project->getProperty('phing.file')
358 30
                && $this->getOwningTarget() !== null
359
            ) {
360 17
                $owningTargetName = $this->getOwningTarget()->getName();
361 17
                if ($this->newTarget === $owningTargetName) {
362 2
                    throw new BuildException(
363 2
                        sprintf(
364 2
                            "%s task calling its own parent target",
365 2
                            $this->getTaskName()
366
                        )
367
                    );
368
                }
369
370 15
                $targets = $this->getProject()->getTargets();
371 15
                $taskName = $this->getTaskName();
372
373 15
                foreach ($this->locals as $local) {
374 15
                    if (isset($targets[$local])) {
375 7
                        if ($targets[$local]->dependsOn($owningTargetName)) {
376
                            throw new BuildException(
377
                                sprintf(
378
                                    "%s task calling a target that depends on its parent target '%s'.",
379
                                    $taskName,
380
                                    $owningTargetName
381
                                )
382
                            );
383
                        }
384
                    }
385
                }
386
            }
387
388 28
            $this->addReferences();
389 28
            $this->newProject->executeTarget($this->newTarget);
390 7
        } catch (\Exception $e) {
391 7
            $buildFailed = true;
392 7
            $this->log($e->getMessage(), Project::MSG_ERR);
393 7
            if (Phing::getMsgOutputLevel() <= Project::MSG_DEBUG) {
394 7
                $lines = explode("\n", $e->getTraceAsString());
395 7
                foreach ($lines as $line) {
396 7
                    $this->log($line, Project::MSG_DEBUG);
397
                }
398
            }
399 27
        } finally {
400
            // reset environment values to prevent side-effects.
401
402 33
            $this->newProject = null;
403 33
            $pkeys = array_keys($this->properties);
404 33
            foreach ($pkeys as $k) {
405 12
                $this->properties[$k]->setProject(null);
406
            }
407
408 33
            if ($this->output !== null && $this->out !== null) {
409 1
                $this->out->close();
410
            }
411
412 33
            $this->dir = $savedDir;
413 33
            $this->phingFile = $savedPhingFile;
414 33
            $this->newTarget = $savedTarget;
415
416
            // If the basedir for any project was changed, we need to set that back here.
417 33
            if ($savedBasedirAbsPath !== null) {
0 ignored issues
show
introduced by
The condition $savedBasedirAbsPath !== null is always false.
Loading history...
418
                chdir($savedBasedirAbsPath);
419
            }
420
421 33
            if ($this->haltOnFailure && $buildFailed) {
422 33
                throw new BuildException('Execution of the target buildfile failed. Aborting.');
423
            }
424
        }
425 27
    }
426
427
    /**
428
     * Get the (sub)-Project instance currently in use.
429
     *
430
     * @return Project
431
     */
432 33
    protected function getNewProject(): \Phing\Project
433
    {
434 33
        if ($this->newProject === null) {
435 6
            $this->reinit();
436
        }
437 33
        return $this->newProject;
438
    }
439
440
    /**
441
     * Configure the Project, i.e. make intance, attach build listeners
442
     * (copy from father project), add Task and Datatype definitions,
443
     * copy properties and references from old project if these options
444
     * are set via the attributes of the XML tag.
445
     *
446
     * Developer note:
447
     * This function replaces the old methods "init", "_reinit" and
448
     * "_initializeProject".
449
     */
450 33
    private function initializeProject()
451
    {
452 33
        $this->newProject->setInputHandler($this->project->getInputHandler());
453
454 33
        foreach ($this->project->getBuildListeners() as $listener) {
455 33
            $this->newProject->addBuildListener($listener);
456
        }
457
458
        /* Copy things from old project. Datatypes and Tasks are always
459
         * copied, properties and references only if specified so/not
460
         * specified otherwise in the XML definition.
461
         */
462
        // Add Datatype definitions
463 33
        foreach ($this->project->getDataTypeDefinitions() as $typeName => $typeClass) {
464 33
            $this->newProject->addDataTypeDefinition($typeName, $typeClass);
465
        }
466
467
        // Add Task definitions
468 33
        foreach ($this->project->getTaskDefinitions() as $taskName => $taskClass) {
469 33
            if ($taskClass === 'phing.tasks.system.PropertyTask') {
470
                // we have already added this taskdef in init()
471
                continue;
472
            }
473 33
            $this->newProject->addTaskDefinition($taskName, $taskClass);
474
        }
475
476 33
        if ($this->output !== null) {
477
            try {
478 1
                if ($this->dir !== null) {
479 1
                    $outfile = (new FileUtils())->resolveFile($this->dir, $this->output);
480
                } else {
481 1
                    $outfile = $this->getProject()->resolveFile($this->output);
482
                }
483 1
                $this->out = new FileOutputStream($outfile);
484 1
                $logger = new DefaultLogger();
485 1
                $logger->setMessageOutputLevel(Project::MSG_INFO);
486 1
                $logger->setOutputStream($this->out);
487 1
                $logger->setErrorStream($this->out);
488 1
                $this->newProject->addBuildListener($logger);
489
            } catch (\Exception $ex) {
490
                $this->log("Phing: Can't set output to " . $this->output);
491
            }
492
        }
493
494 33
        if ($this->useNativeBasedir) {
495
            $this->addAlmostAll($this->getProject()->getUserProperties(), 'user');
496
        } else {
497 33
            $this->project->copyUserProperties($this->newProject);
498
        }
499
500 33
        if (!$this->inheritAll) {
501
            // set System built-in properties separately,
502
            // b/c we won't inherit them.
503 13
            $this->newProject->setSystemProperties();
504
        } else {
505 21
            $this->addAlmostAll($this->getProject()->getProperties(), 'plain');
506
        }
507 33
    }
508
509
    /**
510
     * Copies all properties from the given table to the new project -
511
     * omitting those that have already been set in the new project as
512
     * well as properties named basedir or phing.file.
513
     * @param array $props properties <code>Hashtable</code> to copy to the
514
     * new project.
515
     * @param string $type the type of property to set (a plain Phing property, a
516
     * user property or an inherited property).
517
     */
518 21
    private function addAlmostAll(array $props, string $type): void
519
    {
520 21
        foreach ($props as $name => $value) {
521 21
            if ($name === 'basedir' || $name === 'phing.file' || $name === 'phing.version') {
522
                // basedir and phing.file get special treatment in main()
523 21
                continue;
524
            }
525 21
            if ($type === 'plain') {
526
                // don't re-set user properties, avoid the warning message
527 21
                if ($this->newProject->getProperty($name) === null) {
528
                    // no user property
529 21
                    $this->newProject->setNewProperty($name, $value);
530
                }
531
            } elseif ($type === 'user') {
532
                $this->newProject->setUserProperty($name, $value);
533
            } elseif ($type === 'inherited') {
534
                $this->newProject->setInheritedProperty($name, $value);
535
            }
536
        }
537 21
    }
538
539
    /**
540
     * Override the properties in the new project with the one
541
     * explicitly defined as nested elements here.
542
     *
543
     * @return void
544
     * @throws BuildException
545
     */
546 33
    private function overrideProperties()
547
    {
548
        // remove duplicate properties - last property wins
549 33
        $properties = array_reverse($this->properties);
550 33
        $set = [];
551 33
        foreach ($properties as $i => $p) {
552 12
            if ($p->getName() !== null && $p->getName() !== '') {
553 12
                if (in_array($p->getName(), $set)) {
554 7
                    unset($this->properties[$i]);
555
                } else {
556 12
                    $set[] = $p->getName();
557
                }
558
            }
559 12
            $p->setProject($this->newProject);
560 12
            $p->main();
561
        }
562 33
        if ($this->useNativeBasedir) {
563
            $this->addAlmostAll($this->getProject()->getInheritedProperties(), 'inherited');
564
        } else {
565 33
            $this->project->copyInheritedProperties($this->newProject);
566
        }
567 33
    }
568
569
    /**
570
     * Add the references explicitly defined as nested elements to the
571
     * new project.  Also copy over all references that don't override
572
     * existing references in the new project if inheritrefs has been
573
     * requested.
574
     *
575
     * @return void
576
     * @throws BuildException
577
     */
578 28
    private function addReferences()
579
    {
580
581
        // parent project references
582 28
        $projReferences = $this->project->getReferences();
583
584 28
        $newReferences = $this->newProject->getReferences();
585
586 28
        $subprojRefKeys = [];
587
588 28
        if (count($this->references) > 0) {
589
            for ($i = 0, $count = count($this->references); $i < $count; $i++) {
590
                /** @var Reference $ref */
591
                $ref = $this->references[$i];
592
                $refid = $ref->getRefId();
593
594
                if ($refid === null) {
595
                    throw new BuildException('the refid attribute is required for reference elements');
596
                }
597
                if (!isset($projReferences[$refid])) {
598
                    $this->log("Parent project doesn't contain any reference '" . $refid . "'", Project::MSG_WARN);
599
                    continue;
600
                }
601
602
                $subprojRefKeys[] = $refid;
603
                unset($this->references[$i]);//thisReferences.remove(refid);
604
                $toRefid = $ref->getToRefid();
0 ignored issues
show
Bug introduced by
The method getToRefid() does not exist on Phing\Type\Reference. Did you maybe mean getRefId()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

604
                /** @scrutinizer ignore-call */ 
605
                $toRefid = $ref->getToRefid();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
605
                if ($toRefid === null) {
606
                    $toRefid = $refid;
607
                }
608
                $this->copyReference($refid, $toRefid);
609
            }
610
        }
611
612
        // Now add all references that are not defined in the
613
        // subproject, if inheritRefs is true
614 28
        if ($this->inheritRefs) {
615
            // get the keys that are were not used by the subproject
616 2
            $unusedRefKeys = array_diff(array_keys($projReferences), $subprojRefKeys);
617
618 2
            foreach ($unusedRefKeys as $key) {
619 2
                if (isset($newReferences[$key])) {
620 2
                    continue;
621
                }
622 2
                $this->copyReference($key, $key);
623
            }
624
        }
625 28
    }
626
627
    /**
628
     * Try to clone and reconfigure the object referenced by oldkey in
629
     * the parent project and add it to the new project with the key
630
     * newkey.
631
     *
632
     * <p>If we cannot clone it, copy the referenced object itself and
633
     * keep our fingers crossed.</p>
634
     *
635
     * @param  string $oldKey
636
     * @param  string $newKey
637
     * @throws BuildException
638
     * @return void
639
     */
640 2
    private function copyReference($oldKey, $newKey)
641
    {
642 2
        $orig = $this->project->getReference($oldKey);
643 2
        if ($orig === null) {
644
            $this->log(
645
                "No object referenced by " . $oldKey . ". Can't copy to "
646
                . $newKey,
647
                Project::MSG_WARN
648
            );
649
650
            return;
651
        }
652
653 2
        $copy = clone $orig;
654
655 2
        if ($copy instanceof ProjectComponent) {
656 2
            $copy->setProject($this->newProject);
657 2
        } elseif (in_array('setProject', get_class_methods(get_class($copy)))) {
658
            $copy->setProject($this->newProject);
659 2
        } elseif (!($copy instanceof Project)) {
660
            // don't copy the old "Project" itself
661
            $msg = "Error setting new project instance for "
662
                . "reference with id " . $oldKey;
663
            throw new BuildException($msg);
664
        }
665
666 2
        $this->newProject->addReference($newKey, $copy);
667 2
    }
668
669
    /**
670
     * If true, pass all properties to the new phing project.
671
     * Defaults to true.
672
     *
673
     * @param $value
674
     */
675 20
    public function setInheritAll($value)
676
    {
677 20
        $this->inheritAll = (bool) $value;
678 20
    }
679
680
    /**
681
     * If true, pass all references to the new phing project.
682
     * Defaults to false.
683
     *
684
     * @param $value
685
     */
686 12
    public function setInheritRefs($value)
687
    {
688 12
        $this->inheritRefs = (bool) $value;
689 12
    }
690
691
    /**
692
     * The directory to use as a base directory for the new phing project.
693
     * Defaults to the current project's basedir, unless inheritall
694
     * has been set to false, in which case it doesn't have a default
695
     * value. This will override the basedir setting of the called project.
696
     *
697
     * @param File $d
698
     */
699 12
    public function setDir(File $d): void
700
    {
701 12
        $this->dir = $d;
702 12
    }
703
704
    /**
705
     * The build file to use.
706
     * Defaults to "build.xml". This file is expected to be a filename relative
707
     * to the dir attribute given.
708
     *
709
     * @param $s
710
     */
711 33
    public function setPhingFile($s)
712
    {
713
        // it is a string and not a file to handle relative/absolute
714
        // otherwise a relative file will be resolved based on the current
715
        // basedir.
716 33
        $this->phingFile = $s;
717 33
    }
718
719
    /**
720
     * Alias function for setPhingfile
721
     *
722
     * @param $s
723
     */
724 8
    public function setBuildfile($s)
725
    {
726 8
        $this->setPhingFile($s);
727 8
    }
728
729
    /**
730
     * The target of the new Phing project to execute.
731
     * Defaults to the new project's default target.
732
     *
733
     * @param string $s
734
     */
735 32
    public function setTarget(string $s)
736
    {
737 32
        if ('' === $s) {
738 1
            throw new BuildException("target attribute must not be empty");
739
        }
740
741 31
        $this->newTarget = $s;
0 ignored issues
show
Documentation Bug introduced by
It seems like $s of type string is incompatible with the declared type Phing\Target of property $newTarget.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
742 31
    }
743
744
    /**
745
     * Set the filename to write the output to. This is relative to the value
746
     * of the dir attribute if it has been set or to the base directory of the
747
     * current project otherwise.
748
     * @param string $outputFile the name of the file to which the output should go.
749
     */
750 1
    public function setOutput(string $outputFile): void
751
    {
752 1
        $this->output = $outputFile;
753 1
    }
754
755
    /**
756
     * Property to pass to the new project.
757
     * The property is passed as a 'user property'
758
     */
759 12
    public function createProperty()
760
    {
761 12
        $p = new PropertyTask();
762 12
        $p->setFallback($this->getNewProject());
763 12
        $p->setUserProperty(true);
764 12
        $p->setTaskName('property');
765 12
        $this->properties[] = $p;
766
767 12
        return $p;
768
    }
769
770
    /**
771
     * Reference element identifying a data type to carry
772
     * over to the new project.
773
     *
774
     * @param PhingReference $ref
775
     */
776 1
    public function addReference(PhingReference $ref)
777
    {
778 1
        $this->references[] = $ref;
779 1
    }
780
}
781