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

PackageTask::main()   D

Complexity

Conditions 10
Paths 12

Size

Total Lines 140
Code Lines 103

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 103
nc 12
nop 0
dl 0
loc 140
rs 4.8196
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
// +---------------------------------------------------------------------------+
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
17
use Agavi\Build\Exception\BuildException;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BuildException.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
19
class PackageTask extends Task
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...
20
{
21
	private $baseDir;
22
	private $buildDir;
23
	
24
	public function setBaseDir($baseDir)
25
	{
26
		$this->baseDir = (string) $baseDir;
27
	}
28
	
29
	public function setBuildDir($buildDir)
30
	{
31
		$this->buildDir = (string) $buildDir;
32
	}
33
	
34
	public function main()
35
	{
36
		require_once('PEAR/PackageFileManager2.php');
37
		require_once('PEAR/Exception.php');
38
		PEAR::setErrorHandling(PEAR_ERROR_CALLBACK,'PEAR_ErrorToPEAR_Exception');
39
		if(!$this->baseDir || !file_exists($this->baseDir)) {
40
			throw new BuildException('Base directory is not defined or does not exist.');
41
		}
42
		
43
		if(!$this->buildDir || !file_exists($this->buildDir)) {
44
			throw new BuildException('Build directory is not defined or does not exist.');
45
		}
46
		
47
		set_time_limit(0);
48
		
49
		$this->log("Adding .keep files to empty directories", PROJECT_MSG_INFO);
50
		
51
		foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(realpath('samples')), RecursiveIteratorIterator::CHILD_FIRST) as $dir) {
52
			if($dir->isDir()) {
53
				foreach(new DirectoryIterator($dir->getPathname()) as $d) {
54
					if(!in_array($d->getFilename(), array('.', '..'))) {
55
						continue 2;
56
					}
57
				}
58
				touch($dir->getPathname() . '/.keep');
59
			}
60
		}
61
		
62
		$this->log("Building package contents in: {$this->dir}", PROJECT_MSG_INFO);
0 ignored issues
show
Bug introduced by
The property dir does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
63
		
64
		$version = $this->project->getProperty('agavi.pear.version');
65
		$status = $this->project->getProperty('agavi.status');
66
		
67
		// Modify short description. Try to keep under 80 chars width
68
$shortDesc = <<<EOD
69
PHP5 MVC Application Framework
70
EOD;
71
72
		// Modify long description. Try to keep under 80 chars width
73
$longDesc = <<<EOD
74
Agavi is a full-featured MVC-style framework for PHP5 with a strong focus on structure, code reusability and flexibility.
75
EOD;
76
77
		$p2 = new PEAR_PackageFileManager2;
78
		$p2->setOptions(array(
79
			'filelistgenerator' => 'file',
80
			'outputdirectory' => $this->baseDir,
81
			'packagedirectory' => $this->buildDir,
82
			'baseinstalldir' => 'agavi',
83
			'ignore' => array(
84
				'.svn/',
85
			),
86
			'addhiddenfiles' => true,
87
			'dir_roles' => array(
88
				'/' => 'php',
89
				'bin' => 'script',
90
				'samples' => 'data',
91
			),
92
			'installexceptions' => array(
93
				'bin/agavi-dist' => '/',
94
				'bin/agavi.bat-dist' => '/',
95
			),
96
			'exceptions' => array(
97
				'API_CHANGELOG' => 'doc',
98
				'CHANGELOG' => 'doc',
99
				'COPYRIGHT' => 'doc',
100
				'INSTALL' => 'doc',
101
				'LICENSE' => 'doc',
102
				'LICENSE-AGAVI' => 'doc',
103
				'LICENSE-ICU' => 'doc',
104
				'LICENSE-SCHEMATRON' => 'doc',
105
				'LICENSE-UNICODE_CLDR' => 'doc',
106
				'RELEASE_NOTES' => 'doc',
107
				'UPGRADING' => 'doc',
108
			),
109
		));
110
		$p2->setPackageType('php');
111
		$p2->setPackage('agavi');
112
		$p2->addMaintainer('lead', 'david', 'David Zülke', '[email protected]');
113
		$p2->addMaintainer('developer', 'dominik', 'Dominik del Bondio', '[email protected]');
114
		$p2->addMaintainer('developer', 'felix', 'Felix Gilcher', '[email protected]');
115
		$p2->addMaintainer('developer', 'impl', 'Noah Fontes', '[email protected]');
116
		$p2->addMaintainer('developer', 'v-dogg', 'Veikko Mäkinen', '[email protected]');
117
		$p2->setChannel('pear.agavi.org');
118
		$p2->setReleaseVersion($version);
119
		$p2->setAPIVersion($version);
120
		$p2->setReleaseStability($status);
121
		$p2->setAPIStability($status);
122
		$p2->setSummary($shortDesc);
123
		$p2->setDescription($longDesc);
124
		$p2->setNotes("To see what's new, please refer to the RELEASE_NOTES. Also, the CHANGELOG contains a full list of changes.\n\nFor installation instructions, consult INSTALL. Information on how to migrate applications written using previous releases can be found in UPGRADING.");
125
		
126
		// this must be the most stupid syntax I've ever seen.
127
		$p2->addRelease();
128
		$p2->setOSInstallCondition('windows');
129
		$p2->addInstallAs('bin/agavi.bat-dist', 'agavi.bat');
130
		$p2->addIgnoreToRelease('bin/agavi-dist');
131
		
132
		// and the next release... very cool, eh? how utterly stupid is that
133
		$p2->addRelease();
134
		$p2->addInstallAs('bin/agavi-dist', 'agavi');
135
		$p2->addIgnoreToRelease('bin/agavi.bat-dist');
136
		
137
		$p2->addPackageDepWithChannel('required', 'phing', 'pear.phing.info', '2.4.0');
138
		$p2->addPackageDepWithChannel('optional', 'PHPUnit', 'pear.phpunit.de', '3.7.0');
139
		
140
		$p2->addConflictingPackageDepWithChannel('phing', 'pear.php.net');
141
		
142
		$p2->setPhpDep('5.2.0');
143
		
144
		$p2->addExtensionDep('required', 'dom');
145
		$p2->addExtensionDep('required', 'libxml');
146
		$p2->addExtensionDep('required', 'SPL');
147
		$p2->addExtensionDep('required', 'Reflection');
148
		$p2->addExtensionDep('required', 'pcre');
149
		$p2->addExtensionDep('optional', 'xsl');
150
		$p2->addExtensionDep('optional', 'tokenizer');
151
		$p2->addExtensionDep('optional', 'session');
152
		$p2->addExtensionDep('optional', 'xmlrpc');
153
		$p2->addExtensionDep('optional', 'PDO');
154
		$p2->addExtensionDep('optional', 'iconv');
155
		$p2->addExtensionDep('optional', 'gettext');
156
		
157
		$p2->setPearinstallerDep('1.4.0');
158
		
159
		$p2->setLicense('LGPL', 'http://www.gnu.org/copyleft/lesser.html');
160
		
161
		$p2->addReplacement('bin/agavi-dist', 'pear-config', '@PEAR-DIR@', 'php_dir');
162
		$p2->addReplacement('bin/agavi-dist', 'pear-config', '@PHP-BIN@', 'php_bin');
163
		$p2->addReplacement('bin/agavi.bat-dist', 'pear-config', '@PEAR-DIR@', 'php_dir');
164
		$p2->addReplacement('bin/agavi.bat-dist', 'pear-config', '@PHP-BIN@', 'php_bin');
165
		$p2->addReplacement('src/build/build.xml', 'pear-config', '@PEAR-DIR@', 'php_dir');
166
		$p2->generateContents();
167
		
168
		try {
169
			$p2->writePackageFile();
170
		} catch(PEAR_Exception $e) {
0 ignored issues
show
Bug introduced by
The class PEAR_Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
171
			$this->log("Oops! Caught PEAR Exception: ".$e->getMessage());
172
		}
173
	}
174
}
175
176
function PEAR_ErrorToPEAR_Exception($err)
177
{
178
	if($err->getCode()) {
179
		throw new PEAR_Exception($err->getMessage(), $err->getCode());
180
	}
181
	throw new PEAR_Exception($err->getMessage());
182
}
183
184
?>
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...