Skip to content

Commit 4168653

Browse files
committed
small fix for nested arrays and build createPackage.php for creating PEAR packages
1 parent 8a42368 commit 4168653

22 files changed

+371
-58
lines changed

PHP/CodeFormatter/Autoload.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<?php // this is an autogenerated file - do not edit (created Thu, 15 Sep 2011 12:54:42 +0200)
1+
<?php
2+
// @codingStandardsIgnoreFile
3+
// @codeCoverageIgnoreStart
4+
// this is an autogenerated file - do not edit
25
spl_autoload_register(
36
function($class) {
47
static $classes = null;
@@ -24,3 +27,4 @@ function($class) {
2427
}
2528
}
2629
);
30+
// @codeCoverageIgnoreEnd

PHP/CodeFormatter/Command.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static function main()
8282
{
8383
self::$parser = new \Console_CommandLine();
8484
self::$parser->description = 'PHP_CodeFormatter 0.1 by Dennis Becker';
85-
self::$parser->version = '0.1';
85+
self::$parser->version = '@PACKAGE_VERSION@';
8686
self::$parser->addOption('standard', array(
8787
'long_name' => '--standard',
8888
'description' => 'Coding Standard like PEAR',
@@ -146,12 +146,14 @@ private function run($codingStandard, $directory, $outputDirectory = null)
146146
protected static function parseFile($file)
147147
{
148148
echo $file->getPathname()."\n";
149+
149150
$source = file_get_contents($file->getPathname());
150151
$formattedSourceCode = self::$formatter->format($source);
151152

152153
$path = explode('/', $file->getPath());
153154
$folderStructureCount = count($path);
154-
if ($folderStructureCount > 1) {
155+
156+
if ($folderStructureCount > 1 && $path[0] != '') {
155157
$path[0] = self::$outputDirectory.$path[0];
156158
self::createDirectory($path[0]);
157159
}
@@ -170,4 +172,4 @@ protected static function createDirectory($path)
170172
mkdir($path);
171173
}
172174
}
173-
}
175+
}

PHP/CodeFormatter/Formatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ public function __construct(Tokenizer $tokenizer, AbstractStandard $standard)
9191
public function format($source)
9292
{
9393
$tokens = $this->tokenizer->tokenize($source);
94+
9495
$this->standard->resetIndentation();
9596
$this->lines = array();
9697
$this->line = new SourceCodeLine($this->standard->getIndentationCharacter(),
9798
$this->standard->getIndentationWidth(), 0);
98-
9999
foreach ($tokens as $token) {
100100
$tokenName = $token->getName();
101101
$methodName = $this->buildMethodName($tokenName);

PHP/CodeFormatter/Standards/Pear.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class Pear extends AbstractStandard {
6969
'T_FUNCTION_CLOSE_CURLY_BRACKET',
7070
'T_IF_OPEN_CURLY_BRACKET',
7171
'T_IF_CLOSE_CURLY_BRACKET',
72+
'T_ELSE_OPEN_CURLY_BRACKET',
73+
'T_ELSE_CLOSE_CURLY_BRACKET',
74+
'T_ELSEIF_OPEN_CURLY_BRACKET',
75+
'T_ELSEIF_CLOSE_CURLY_BRACKET',
7276
'T_FOR_OPEN_CURLY_BRACKET',
7377
'T_FOR_CLOSE_CURLY_BRACKET',
7478
'T_FOREACH_OPEN_CURLY_BRACKET',
@@ -108,6 +112,8 @@ class Pear extends AbstractStandard {
108112
'T_FOR_OPEN_CURLY_BRACKET',
109113
'T_FOREACH_OPEN_CURLY_BRACKET',
110114
'T_SWITCH_OPEN_CURLY_BRACKET',
115+
'T_ELSE',
116+
'T_ELSEIF',
111117
'T_PLUS',
112118
'T_MINUS',
113119
'T_EQUAL',
@@ -148,6 +154,8 @@ class Pear extends AbstractStandard {
148154
'T_FUNCTION',
149155
'T_COMMA',
150156
'T_IF',
157+
'T_ELSE',
158+
'T_ELSEIF',
151159
'T_FOR',
152160
'T_FOREACH',
153161
'T_SWITCH',
@@ -193,21 +201,27 @@ class Pear extends AbstractStandard {
193201
'T_CLASS_OPEN_CURLY_BRACKET',
194202
'T_FUNCTION_OPEN_CURLY_BRACKET',
195203
'T_IF_OPEN_CURLY_BRACKET',
204+
'T_ELSE_OPEN_CURLY_BRACKET',
205+
'T_ELSEIF_OPEN_CURLY_BRACKET',
196206
'T_TRY_OPEN_CURLY_BRACKET',
197207
'T_CATCH_OPEN_CURLY_BRACKET',
198208
'T_FOR_OPEN_CURLY_BRACKET',
199209
'T_FOREACH_OPEN_CURLY_BRACKET',
200210
'T_ARRAY_OPEN_ROUND_BRACKET',
211+
'T_SWITCH_OPEN_CURLY_BRACKET',
201212
);
202213
protected $decreaseThisLine = array(
203214
'T_CLASS_CLOSE_CURLY_BRACKET',
204215
'T_FUNCTION_CLOSE_CURLY_BRACKET',
205216
'T_IF_CLOSE_CURLY_BRACKET',
217+
'T_ELSE_CLOSE_CURLY_BRACKET',
218+
'T_ELSEIF_CLOSE_CURLY_BRACKET',
206219
'T_TRY_CLOSE_CURLY_BRACKET',
207220
'T_CATCH_CLOSE_CURLY_BRACKET',
208221
'T_FOR_CLOSE_CURLY_BRACKET',
209222
'T_FOREACH_CLOSE_CURLY_BRACKET',
210223
'T_ARRAY_CLOSE_ROUND_BRACKET',
224+
'T_SWITCH_CLOSE_CURLY_BRACKET',
211225
);
212226
protected $decreaseNextLine = array();
213227

PHP/CodeFormatter/Token.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ public function getContent() {
112112
return $this->content;
113113
}
114114

115+
/**
116+
* Get original line of source code
117+
*
118+
* @return int
119+
*/
120+
public function getLine() {
121+
return $this->line;
122+
}
123+
115124
/**
116125
* Get previous Token object
117126
*

PHP/CodeFormatter/Tokenizer.php

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Tokenizer
7171
protected $curlyBrackets = array(
7272
'T_CLASS',
7373
'T_INTERFACE',
74-
'T_CASE',
74+
// 'T_CASE',
7575
'T_TRY',
7676
'T_ELSE',
7777
'T_FUNCTION_CLOSE_ROUND_BRACKET',
@@ -96,7 +96,9 @@ class Tokenizer
9696
'T_ARRAY',
9797
);
9898

99-
99+
protected $noSpecialRoundBrackets = array(
100+
'T_OBJECT_OPERATOR',
101+
);
100102

101103
/**
102104
* Stack of token names to match round brackets
@@ -124,18 +126,20 @@ public function tokenize($source)
124126
continue;
125127
}
126128

127-
$tokenObject = $this->parseToken($sourceToken, $previousToken);
128-
$this->tokenCollection[] = $previousToken = $tokenObject;
129+
$object = $this->parseToken($sourceToken, $previousToken);
130+
$this->tokenCollection[] = $previousToken = $object;
129131
}
130132

131133
return $this->tokenCollection;
132134
}
133135

134136
protected function parseToken($sourceToken, $previousToken) {
135-
$parsedSourceToken = array();
137+
$parsedSourceToken = array();
138+
136139
if (is_array($sourceToken)) {
137140
$parsedSourceToken = $sourceToken;
138141
$parsedSourceToken[3] = token_name($sourceToken[0]);
142+
$this->addTokenOntoStack($parsedSourceToken[3]);
139143
} else {
140144
$parsedSourceToken = $this->addTokenData($sourceToken, $previousToken);
141145
}
@@ -151,7 +155,8 @@ protected function sanitizeCode($tokenName, $previousToken)
151155
{
152156
switch ($tokenName) {
153157
case 'T_ARRAY_CLOSE_ROUND_BRACKET':
154-
if ('T_COMMA_FOR_ARRAY' !== $previousToken->getName()) {
158+
if ('T_COMMA_FOR_ARRAY' != $previousToken->getName()
159+
&& 'T_ARRAY_OPEN_ROUND_BRACKET' != $previousToken->getName()) {
155160
$previousToken = $this->parseToken(',', $previousToken);
156161
$this->tokenCollection[] = $previousToken;
157162
}
@@ -171,9 +176,7 @@ protected function addTokenOntoStack($tokenName)
171176
switch ($tokenName) {
172177
case 'T_CLASS':
173178
case 'T_INTERFACE':
174-
case 'T_CASE':
175179
case 'T_TRY':
176-
case 'T_ELSE':
177180
array_unshift($this->curlyBracketStack, $tokenName);
178181
break;
179182
case 'T_CATCH':
@@ -198,14 +201,9 @@ protected function addTokenOntoStack($tokenName)
198201
protected function addTokenData($sourceString, Token $previousToken)
199202
{
200203
$tokenName = '';
201-
$controlTokenName = '';
204+
$controlTokenName = null;
202205
switch ($sourceString) {
203206
case '{':
204-
// if(!isset($this->curlyBracketStack[0])) {
205-
// $tokenName = 'T_OPEN_CURLY_BRACKET';
206-
// } else {
207-
// $tokenName = $this->curlyBracketStack[0].'_OPEN_CURLY_BRACKET';
208-
// }
209207
$tokenName = 'T_OPEN_CURLY_BRACKET';
210208

211209
$controlTokenName = $this->findPreviousControlTokenForCurlyBracket($previousToken);
@@ -217,42 +215,25 @@ protected function addTokenData($sourceString, Token $previousToken)
217215
array_unshift($this->curlyBracketStack, $tokenName);
218216
break;
219217
case '}':
220-
// if(empty($this->curlyBracketStack)) {
221-
// $tokenName = 'T_CLOSE_CURLY_BRACKET';
222-
// } else {
223-
// $tokenName = array_shift($this->curlyBracketStack).'_CLOSE_CURLY_BRACKET';
224-
// }
225218
$tokenName = array_shift($this->curlyBracketStack);
226-
//var_dump($tokenName);
227219
$tokenName = str_replace('_OPEN_', '_CLOSE_', $tokenName);
228-
// var_dump($tokenName);
229220
break;
230221
case '(':
231222
$tokenName = 'T_OPEN_ROUND_BRACKET';
223+
224+
if (!function_exists($previousToken->getContent())) {
225+
$controlTokenName = $this->findPreviousControlTokenForRoundBracket($previousToken);
226+
}
232227

233-
$controlTokenName = $this->findPreviousControlTokenForRoundBracket($previousToken);
234-
// var_dump($sourceString);
235-
// var_dump($previousToken->getPreviousToken()->getName());
236-
// var_dump($previousToken->getPreviousToken()->getContent());
237-
// var_dump($controlTokenName);
238-
// die();
239228
if (null !== $controlTokenName) {
240229
$tokenName = $controlTokenName . '_OPEN_ROUND_BRACKET';
241230
}
242-
// if (in_array($previousToken->getName(), $this->roundBrackets)) {
243-
// $tokenName = $previousToken->getName().'_OPEN_ROUND_BRACKET';
244-
// }
245-
// var_dump($sourceString);
246-
// var_dump($tokenName);
247-
// die();
231+
248232
array_unshift($this->roundBracketStack, $tokenName);
249233
break;
250234
case ')':
251-
$tokenName = str_replace('_OPEN_', '_CLOSE_', array_shift($this->roundBracketStack));
252-
// var_dump($tokenName);
253-
// if (in_array($tokenName, $this->curlyBrackets)) {
254-
// array_unshift($this->curlyBracketStack, str_replace('_CLOSE_ROUND_BRACKET', '', $tokenName));
255-
// }
235+
$tokenName = array_shift($this->roundBracketStack);
236+
$tokenName = str_replace('_OPEN_', '_CLOSE_', $tokenName);
256237
break;
257238
case ',':
258239
$tokenName = 'T_COMMA' . $this->contextPostfix;
@@ -354,6 +335,8 @@ protected function findPreviousControlTokenForCurlyBracket(Token $token) {
354335
protected function findPreviousControlTokenForRoundBracket(Token $token) {
355336
if (in_array($token->getName(), $this->roundBrackets)) {
356337
return $token->getName();
338+
} elseif (in_array($token->getName(), $this->noSpecialRoundBrackets)) {
339+
return null;
357340
} elseif (false !== strpos($token->getName(), 'OPEN_ROUND')) {
358341
return null;
359342
} elseif (false !== strpos($token->getName(), 'CLOSE_ROUND')) {

createPackage.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/php
2+
<?php
3+
require_once('PEAR/PackageFileManager2.php');
4+
PEAR::setErrorHandling(PEAR_ERROR_DIE);
5+
6+
$options = array(
7+
'filelistgenerator' => 'file', // this copy of our source code is a CVS checkout
8+
'simpleoutput' => true,
9+
'baseinstalldir' => '/', // The PEAR directory install location
10+
'packagedirectory' => dirname(__FILE__), // We’ve put this file in the root source code dir
11+
'clearcontents' => true, // dump any old package.xml content (set false to append release)
12+
// no bundling of cvs/svn files or this generator file
13+
'ignore' => array('createPackage.php', 'build.xml', 'phpunit.xml.dist', 'README.markdown', '.git/', 'build/', 'tests/'),
14+
'dir_roles' => array(
15+
'tests' => 'test',
16+
),
17+
);
18+
19+
// Oddly enough, this is a PHP source code package…
20+
21+
$packagexml = &PEAR_PackageFileManager2::importOptions('package.xml', $options);
22+
$packagexml->setPackageType('php');
23+
24+
// Package name, summary and longer description
25+
26+
$packagexml->setPackage('PHP_CodeFormatter');
27+
$packagexml->setSummary('PHP_CodeFormatter let`s you re-format your code so that it matches your coding standards');
28+
$packagexml->setDescription('PHP_CodeFormatter is designed to replace PHP_Beautifier. PHP_Beautifier has limitations for your coding standards, especially if you want to have different formats for curly braces for methods, classes etc. which PHP_CodeFormatter addresses and gives you a solution.');
29+
30+
// The channel where this package is hosted. Since we’re installing from a local
31+
// downloaded file rather than a channel we’ll pretend it’s from PEAR.
32+
33+
$packagexml->setChannel('dennisbecker.github.com/pear');
34+
35+
// Add some release notes!
36+
37+
$notes = "- 0.1.0 alpha version for testing";
38+
39+
$packagexml->setNotes($notes);
40+
41+
// Add file base changes
42+
43+
$packagexml->addGlobalReplacement('pear-config', '@PHP_BIN@', 'php_bin');
44+
$packagexml->addGlobalReplacement('package-info', '@PACKAGE_VERSION@', 'version');
45+
$packagexml->addRole('sh', 'script');
46+
47+
// Add any known dependencies such as PHP version, extensions, PEAR installer
48+
49+
$packagexml->setPhpDep('5.3');
50+
$packagexml->setPearinstallerDep('1.4.0');
51+
$packagexml->addPackageDepWithChannel('required', 'PEAR', 'pear.php.net', '1.4.0');
52+
$packagexml->addPackageDepWithChannel('required', 'Console_CommandLine', 'pear.php.net', '1.1.3');
53+
$packagexml->addPackageDepWithChannel('required', 'PHP_Timer', 'pear.phpunit.de', '1.0.2');
54+
$packagexml->addPackageDepWithChannel('required', 'DirectoryScanner', 'pear.netpirates.net', '1.0.2');
55+
56+
// Other info, like the Lead Developers. license, version details and stability type
57+
58+
$packagexml->addMaintainer('lead', 'DennisBecker', 'Dennis Becker', 'mail.dennisbecker@gmail.com');
59+
$packagexml->setLicense('New BSD License', 'http://opensource.org/licenses/bsd-license.php');
60+
$packagexml->setAPIVersion('0.1.0');
61+
$packagexml->setReleaseVersion('0.1.0');
62+
$packagexml->setReleaseStability('alpha');
63+
$packagexml->setAPIStability('alpha');
64+
65+
// Add this as a release, and generate XML content
66+
67+
$packagexml->addRelease();
68+
$packagexml->addInstallAs('phpcf.sh', 'phpcf');
69+
70+
$packagexml->generateContents();
71+
72+
// Pass a “make” flag from the command line or browser address to actually write
73+
// package.xml to disk, otherwise just debug it for any errors
74+
75+
if (isset($_GET['make']) || (isset($_SERVER['argv']) && @$_SERVER['argv'][1] == 'make')) {
76+
$packagexml->writePackageFile();
77+
} else {
78+
$packagexml->debugPackageFile();
79+
}

0 commit comments

Comments
 (0)