Completed
Pull Request — master (#7)
by Markus
06:25
created

SelectpathTask   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setProperty() 0 4 1
A setPath() 0 10 2
A setType() 0 4 1
A createFrom() 0 6 1
C main() 0 25 11
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
// +---------------------------------------------------------------------------+
4
// | This file is part of the Agavi package.                                   |
5
// | Copyright (c) 2005-2011 the Agavi Project.                                |
6
// |                                                                           |
7
// | For the full copyright and license information, please view the LICENSE   |
8
// | file that was distributed with this source code. You can also view the    |
9
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
10
// |   vi: set noexpandtab:                                                    |
11
// |   Local Variables:                                                        |
12
// |   indent-tabs-mode: t                                                     |
13
// |   End:                                                                    |
14
// +---------------------------------------------------------------------------+
15
16
require_once(__DIR__ . '/AgaviTask.php');
17
18
/**
19
 * Selects the first available file from a list of paths.
20
 *
21
 * @package    agavi
22
 * @subpackage build
23
 *
24
 * @author     Noah Fontes <[email protected]>
25
 * @copyright  Authors
26
 * @copyright  The Agavi Project
27
 *
28
 * @since      1.0.0
29
 *
30
 * @version    $Id$
31
 */
32
class SelectpathTask extends AgaviTask
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
33
{
34
	const TYPE_FILE = 'file';
35
	const TYPE_DIRECTORY = 'directory';
36
	
37
	protected $property = null;
38
	protected $path = null;
39
	protected $type = null;
40
	protected $froms = array();
41
	
42
	/**
43
	 * Sets the property that this task will modify.
44
	 *
45
	 * @param      string The property to modify.
46
	 */
47
	public function setProperty($property)
48
	{
49
		$this->property = $property;
50
	}
51
	
52
	/**
53
	 * Sets the path to locate.
54
	 *
55
	 * @param      string The path to locate.
56
	 */
57
	public function setPath($path)
58
	{
59
		if(!empty($path)) {
60
			/* This must be created here to prevent the directory from
61
			 * becoming automatically converted to an absolute path. */
62
			$this->path = new PhingFile($path);			
63
		} else {
64
			$this->path = null;
65
		}
66
	}
67
	
68
	/**
69
	 * Sets the type that the path must have.
70
	 *
71
	 * @param      string One of <code>file</code> or <code>directory</code>.
72
	 */
73
	public function setType($type)
74
	{
75
		$this->type = $type;
76
	}
77
	
78
	/**
79
	 * Adds a new path to the search list.
80
	 *
81
	 * @param      PhingFile The path to add.
82
	 */
83
	public function createFrom()
84
	{
85
		$from = new FromType();
86
		$this->froms[] = $from;
87
		return $from;
88
	}
89
	
90
	/**
91
	 * Executes the task.
92
	 */
93
	public function main()
94
	{
95
		if($this->property === null) {
96
			throw new \Agavi\Build\Exception\BuildException('The property attribute must be specified');
97
		}
98
		if(count($this->froms) === 0) {
99
			throw new \Agavi\Build\Exception\BuildException('At least one from tag must be specified');
100
		}
101
		
102
		foreach($this->froms as $from) {
103
			if(null !== $this->path) {
104
				$path = new PhingFile($from->getPath()->getAbsolutePath() . DIRECTORY_SEPARATOR . $this->path->getPath());
105
			} else {
106
				$path = new PhingFile($from->getPath()->getAbsolutePath());
107
			}
108
			if(
109
				($this->type === null && file_exists($path->getPath())) ||
110
				($this->type === self::TYPE_FILE && is_file($path->getPath())) ||
111
				($this->type === self::TYPE_DIRECTORY && is_dir($path->getPath()))
112
			) {
113
				$this->project->setUserProperty($this->property, $path->getPath());
114
				return;
115
			}
116
		}
117
	}
118
}
119
120
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...