Completed
Pull Request — master (#7)
by Markus
05:46
created

LocatemoduleTask::main()   C

Complexity

Conditions 11
Paths 14

Size

Total Lines 51
Code Lines 31

Duplication

Lines 19
Ratio 37.25 %

Importance

Changes 0
Metric Value
cc 11
eloc 31
nc 14
nop 0
dl 19
loc 51
rs 5.7333
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
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
 * Locates a module directory given a project and a module name.
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 LocatemoduleTask 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
	protected $property = null;
35
	protected $path = null;
36
	protected $ignoreIfSet = false;
37
	
38
	/**
39
	 * Sets the property that this task will modify.
40
	 *
41
	 * @param      string The property to modify.
42
	 */
43
	public function setProperty($property)
44
	{
45
		$this->property = $property;
46
	}
47
	
48
	/**
49
	 * Sets the path to use to locate the module.
50
	 *
51
	 * @param      string The path to use.
52
	 */
53
	public function setPath(PhingFile $path)
54
	{
55
		$this->path = $path;
56
	}
57
	
58
	/**
59
	 * Sets whether to ignore this check if the property is already set.
60
	 *
61
	 * @param      bool Whether to bypass the check if the property is set.
62
	 */
63
	public function setIgnoreIfSet($ignoreIfSet)
64
	{
65
		$this->ignoreIfSet = StringHelper::booleanValue($ignoreIfSet);
66
	}
67
	
68
	/**
69
	 * Executes the task.
70
	 */
71
	public function main()
72
	{
73
		if($this->property === null) {
74
			throw new \Agavi\Build\Exception\BuildException('The property attribute must be specified');
75
		}
76
		if($this->path === null) {
77
			throw new \Agavi\Build\Exception\BuildException('The path attribute must be specified');
78
		}
79
		
80
		if($this->ignoreIfSet && $this->project->getProperty($this->property) !== null) {
81
			return;
82
		}
83
		
84
		if(!$this->path->exists()) {
85
			throw new \Agavi\Build\Exception\BuildException('The path ' . $this->path->getAbsolutePath() . ' does not exist');
86
		}
87
		$this->path = $this->path->getAbsoluteFile();
88
		if(!$this->path->isDirectory()) {
89
			$this->path = $this->path->getParentFile();
90
		}
91
		
92
		/* Check if the current directory is a project directory. */
93
		$check = new \Agavi\Build\Check\ModuleFilesystemCheck();
94
		$check->setConfigDirectory($this->project->getProperty('module.config.directory'));
95
		
96
		$check->setPath($this->path->getAbsolutePath());
97 View Code Duplication
		if($check->check()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
			/* The current path is the project directory. */
99
			$this->log('Module base directory: ' . $this->path);
100
			$this->project->setUserProperty($this->property, $this->path->getName());
101
			return;
102
		}
103
		
104
		$check->setPath($this->path->getAbsolutePath());
105 View Code Duplication
		if($check->check()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
			$this->log('Module base directory: ' . $this->path);
107
			$this->project->setUserProperty($this->property, $this->path->getName());
108
			return;
109
		}
110
		
111
		/* Last chance: recurse upward and check for a project directory. */
112
		$directory = $this->path;
113 View Code Duplication
		while(($directory = $directory->getParentFile()) !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
			$check->setPath($directory->getAbsolutePath());
115
			if($check->check()) {
116
				$this->log('Module base directory: ' . $directory);
117
				$this->project->setUserProperty($this->property, $directory->getName());
118
				return;
119
			}
120
		}
121
	}
122
}
123
124
?>
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...