Passed
Push — master ( 3372b2...cdab9c )
by Siad
12:15
created

PhingTask::initializeProject()   F

Complexity

Conditions 10
Paths 816

Size

Total Lines 56
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10.2918

Importance

Changes 0
Metric Value
cc 10
eloc 31
c 0
b 0
f 0
nc 816
nop 0
dl 0
loc 56
ccs 24
cts 28
cp 0.8571
crap 10.2918
rs 3.7555

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

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

325
                    /** @scrutinizer ignore-type */ $this->getTaskName()
Loading history...
326
                );
327
            }
328
329 19
            ProjectConfigurator::configureProject($this->newProject, new PhingFile($this->phingFile));
330
331 18
            if ($this->newTarget === null) {
332
                $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 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...
333
            }
334
335
            // Are we trying to call the target in which we are defined?
336
            if (
337 18
                $this->newProject->getBasedir()->equals($this->project->getBasedir())
338 14
                && $this->newProject->getProperty('phing.file') === $this->project->getProperty('phing.file')
339 11
                && $this->getOwningTarget() !== null
340
            ) {
341 11
                $owningTargetName = $this->getOwningTarget()->getName();
342 11
                if ($this->newTarget === $owningTargetName) {
343 2
                    throw new BuildException(
344 2
                        sprintf(
345 2
                            "%s task calling its own parent target",
346 2
                            $this->getTaskName()
347
                        )
348
                    );
349
                }
350
351 9
                $targets = $this->getProject()->getTargets();
352 9
                $taskName = $this->getTaskName();
353
354 9
                foreach ($this->locals as $local) {
355 9
                    if (isset($targets[$local])) {
356 1
                        if ($targets[$local]->dependsOn($owningTargetName)) {
0 ignored issues
show
Bug introduced by
The method dependsOn() does not exist on Target. ( Ignorable by Annotation )

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

356
                        if ($targets[$local]->/** @scrutinizer ignore-call */ dependsOn($owningTargetName)) {

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

585
                /** @scrutinizer ignore-call */ 
586
                $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...
586
                if ($toRefid === null) {
587
                    $toRefid = $refid;
588
                }
589
                $this->copyReference($refid, $toRefid);
590
            }
591
        }
592
593
        // Now add all references that are not defined in the
594
        // subproject, if inheritRefs is true
595 15
        if ($this->inheritRefs) {
596
            // get the keys that are were not used by the subproject
597 2
            $unusedRefKeys = array_diff(array_keys($projReferences), $subprojRefKeys);
598
599 2
            foreach ($unusedRefKeys as $key) {
600 2
                if (isset($newReferences[$key])) {
601 2
                    continue;
602
                }
603 2
                $this->copyReference($key, $key);
604
            }
605
        }
606 15
    }
607
608
    /**
609
     * Try to clone and reconfigure the object referenced by oldkey in
610
     * the parent project and add it to the new project with the key
611
     * newkey.
612
     *
613
     * <p>If we cannot clone it, copy the referenced object itself and
614
     * keep our fingers crossed.</p>
615
     *
616
     * @param  string $oldKey
617
     * @param  string $newKey
618
     * @throws BuildException
619
     * @return void
620
     */
621 2
    private function copyReference($oldKey, $newKey)
622
    {
623 2
        $orig = $this->project->getReference($oldKey);
624 2
        if ($orig === null) {
625
            $this->log(
626
                "No object referenced by " . $oldKey . ". Can't copy to "
627
                . $newKey,
628
                Project::MSG_WARN
629
            );
630
631
            return;
632
        }
633
634 2
        $copy = clone $orig;
635
636 2
        if ($copy instanceof ProjectComponent) {
637 2
            $copy->setProject($this->newProject);
638 2
        } elseif (in_array('setProject', get_class_methods(get_class($copy)))) {
639
            $copy->setProject($this->newProject);
640 2
        } elseif (!($copy instanceof Project)) {
641
            // don't copy the old "Project" itself
642
            $msg = "Error setting new project instance for "
643
                . "reference with id " . $oldKey;
644
            throw new BuildException($msg);
645
        }
646
647 2
        $this->newProject->addReference($newKey, $copy);
648 2
    }
649
650
    /**
651
     * If true, pass all properties to the new phing project.
652
     * Defaults to true.
653
     *
654
     * @param $value
655
     */
656 15
    public function setInheritAll($value)
657
    {
658 15
        $this->inheritAll = (bool) $value;
659 15
    }
660
661
    /**
662
     * If true, pass all references to the new phing project.
663
     * Defaults to false.
664
     *
665
     * @param $value
666
     */
667 11
    public function setInheritRefs($value)
668
    {
669 11
        $this->inheritRefs = (bool) $value;
670 11
    }
671
672
    /**
673
     * The directory to use as a base directory for the new phing project.
674
     * Defaults to the current project's basedir, unless inheritall
675
     * has been set to false, in which case it doesn't have a default
676
     * value. This will override the basedir setting of the called project.
677
     *
678
     * @param PhingFile $d
679
     */
680 3
    public function setDir(PhingFile $d): void
681
    {
682 3
        $this->dir = $d;
683 3
    }
684
685
    /**
686
     * The build file to use.
687
     * Defaults to "build.xml". This file is expected to be a filename relative
688
     * to the dir attribute given.
689
     *
690
     * @param $s
691
     */
692 19
    public function setPhingFile($s)
693
    {
694
        // it is a string and not a file to handle relative/absolute
695
        // otherwise a relative file will be resolved based on the current
696
        // basedir.
697 19
        $this->phingFile = $s;
698 19
    }
699
700
    /**
701
     * Alias function for setPhingfile
702
     *
703
     * @param $s
704
     */
705
    public function setBuildfile($s)
706
    {
707
        $this->setPhingFile($s);
708
    }
709
710
    /**
711
     * The target of the new Phing project to execute.
712
     * Defaults to the new project's default target.
713
     *
714
     * @param string $s
715
     */
716 20
    public function setTarget(string $s)
717
    {
718 20
        if ('' === $s) {
719 1
            throw new BuildException("target attribute must not be empty");
720
        }
721
722 19
        $this->newTarget = $s;
0 ignored issues
show
Documentation Bug introduced by
It seems like $s of type string is incompatible with the declared type 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...
723 19
    }
724
725
    /**
726
     * Set the filename to write the output to. This is relative to the value
727
     * of the dir attribute if it has been set or to the base directory of the
728
     * current project otherwise.
729
     * @param string $outputFile the name of the file to which the output should go.
730
     */
731 1
    public function setOutput(string $outputFile): void
732
    {
733 1
        $this->output = $outputFile;
734 1
    }
735
736
    /**
737
     * Property to pass to the new project.
738
     * The property is passed as a 'user property'
739
     */
740 7
    public function createProperty()
741
    {
742 7
        $p = new PropertyTask();
743 7
        $p->setFallback($this->getNewProject());
744 7
        $p->setUserProperty(true);
745 7
        $p->setTaskName('property');
746 7
        $this->properties[] = $p;
747
748 7
        return $p;
749
    }
750
751
    /**
752
     * Reference element identifying a data type to carry
753
     * over to the new project.
754
     *
755
     * @param PhingReference $ref
756
     */
757 1
    public function addReference(PhingReference $ref)
758
    {
759 1
        $this->references[] = $ref;
760 1
    }
761
}
762