Skip to content

Commit 6eb5901

Browse files
author
abel
committed
tests
1 parent d9e659b commit 6eb5901

File tree

6 files changed

+492
-35
lines changed

6 files changed

+492
-35
lines changed

.php_cs.dist

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
$fileHeaderComment = <<<COMMENT
44
This file is part of the PHPDoc Formatter application.
5+
https://github.com/SinSquare/phpdoc-formatter
56
67
(c) Ábel Katona
78
89
This source file is subject to the MIT license that is bundled with this source code in the file LICENSE.
910
COMMENT;
1011

1112
$finder = PhpCsFixer\Finder::create()
12-
->in(__DIR__.'/src')
13-
->in(__DIR__.'/tests')
14-
->exclude('Resources')
15-
->exclude('Resources2')
13+
->in(__DIR__)
14+
->exclude('tests/Resources')
15+
->exclude('tests/Resources2')
16+
->files()
1617
;
1718

1819
return PhpCsFixer\Config::create()

php-doc-formatter

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,14 @@ $autoloader = false;
103103
foreach (array(__DIR__.'/../../autoload.php', __DIR__.'/../vendor/autoload.php', __DIR__.'/vendor/autoload.php') as $file) {
104104
if (file_exists($file)) {
105105
require_once $file;
106+
$autoloader = true;
106107
break;
107108
}
108109
}
110+
if(!$autoloader) {
111+
error_log('[ERROR] Could not locate the autoloader. Check if you have it.');
112+
exit();
113+
}
109114

110115
use PhpDocFormatter\Application;
111116
use PhpDocFormatter\Config;
@@ -132,6 +137,12 @@ if (is_array($exclude) && count($exclude) > 0) {
132137
}
133138

134139
$config->setFinder($finder);
140+
try {
141+
$config->validate();
142+
} catch(\Exception $ex) {
143+
error_log(sprintf('[ERROR] %s', $ex->getMessage()));
144+
exit();
145+
}
135146

136147
$app = new Application($config);
137148
$app->fixFiles();

phpunit.xml.dist

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="vendor/autoload.php"
13+
>
14+
15+
<php>
16+
<ini name="error_reporting" value="-1" />
17+
</php>
18+
19+
<testsuites>
20+
<testsuite name="PHPDoc Formatter Test Suite">
21+
<directory suffix="Test.php">./tests/</directory>
22+
</testsuite>
23+
</testsuites>
24+
25+
<filter>
26+
<whitelist>
27+
<directory>./</directory>
28+
<exclude>
29+
<directory>./tests</directory>
30+
<directory>./vendor</directory>
31+
</exclude>
32+
</whitelist>
33+
</filter>
34+
35+
<groups>
36+
<exclude>
37+
<group>performance</group>
38+
</exclude>
39+
</groups>
40+
</phpunit>

src/Application.php

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/*
44
* This file is part of the PHPDoc Formatter application.
5+
* https://github.com/SinSquare/phpdoc-formatter
56
*
67
* (c) Ábel Katona
78
*
@@ -12,15 +13,27 @@
1213

1314
use Symfony\Component\Stopwatch\Stopwatch;
1415

16+
/**
17+
* @author Abel Katona
18+
*/
1519
class Application
1620
{
1721
private $config;
1822

23+
/**
24+
* @param Config $config
25+
*
26+
* @throws \Exception if the config is not valid
27+
*/
1928
public function __construct(Config $config)
2029
{
30+
$config->validate();
2131
$this->config = $config;
2232
}
2333

34+
/**
35+
* Fix all the files that the finder returns.
36+
*/
2437
public function fixFiles()
2538
{
2639
$stopwatch = new Stopwatch();
@@ -48,18 +61,11 @@ public function fixFiles()
4861

4962
$norm = $this->getDocBody($value);
5063
$norm = $this->normalizeDocBody($norm);
51-
$norm = $this->formatDocBody($norm, $ident);
64+
$norm = $this->reconstructDocBody($norm, $ident);
5265
$docComments[$key]['formatted'] = $norm;
5366
}
5467

55-
$newFile = '';
56-
$offset = 0;
57-
foreach ($docComments as $key => $value) {
58-
$newFile .= substr($content, $offset, $value['offset'] - $offset);
59-
$offset = $value['offset'] + $value['length'];
60-
$newFile .= $value['formatted'];
61-
}
62-
$newFile .= substr($content, $offset);
68+
$newFile = $this->reconstructFile($file, $docComments);
6369

6470
if (sha1($content) !== sha1($newFile)) {
6571
$d = file_put_contents($file, $newFile);
@@ -72,6 +78,31 @@ public function fixFiles()
7278
}
7379
}
7480

81+
/**
82+
* @param string $file File content
83+
* @param string[] $docComments
84+
*/
85+
private function reconstructFile(string $content, array $docComments)
86+
{
87+
$newFile = '';
88+
$offset = 0;
89+
foreach ($docComments as $key => $value) {
90+
$newFile .= substr($content, $offset, $value['offset'] - $offset);
91+
$offset = $value['offset'] + $value['length'];
92+
$newFile .= $value['formatted'];
93+
}
94+
$newFile .= substr($content, $offset);
95+
96+
return $newFile;
97+
}
98+
99+
/**
100+
* Gets the file's dominant line ending.
101+
*
102+
* @param string $file File content
103+
*
104+
* @return string Dominant line ending
105+
*/
75106
private function getFileDominantLineEnding(string $file)
76107
{
77108
static $eols = array(
@@ -88,33 +119,47 @@ private function getFileDominantLineEnding(string $file)
88119
"\0x0A", // [ASCII] LF: Multics, Unix, Unix-like, BeOS, Amiga, RISC OS
89120
"\0x0D", // [ASCII] CR: Commodore 8-bit, BBC Acorn, TRS-80, Apple II, Mac OS <=v9, OS-9
90121
"\0x1E", // [ASCII] RS: QNX (pre-POSIX)
91-
//"\0x76", // [?????] NEWLINE: ZX80, ZX81 [DEPRECATED]
92122
"\0x15", // [EBCDEIC] NEL: OS/390, OS/400
93123
);
94124
$cur_cnt = 0;
95125
$cur_eol = "\n";
96-
foreach($eols as $eol){
97-
if(($count = substr_count($file, $eol)) > $cur_cnt){
126+
foreach ($eols as $eol) {
127+
if (($count = substr_count($file, $eol)) > $cur_cnt) {
98128
$cur_cnt = $count;
99129
$cur_eol = $eol;
100130
}
101131
}
132+
102133
return $cur_eol;
103134
}
104135

136+
/**
137+
* Gets the opening tag's ident.
138+
*
139+
* @param string $doc The whole unformatted PHPDoc block
140+
*
141+
* @return string The whitespace before the opening tag
142+
*/
105143
private function getDocBodyIdent(string $doc)
106144
{
107145
$lines = explode("\n", $doc);
108146

109147
foreach ($lines as $line) {
110-
if (preg_match("#/\*\*#", $line, $matches, PREG_OFFSET_CAPTURE)) {
111-
return $matches[0][1];
148+
if (preg_match("#([^/\*]*)/\*\*#", $line, $matches)) {
149+
return $matches[1];
112150
}
113151
}
114152

115153
return null;
116154
}
117155

156+
/**
157+
* Gets the body of the PHPDoc without the opening tags.
158+
*
159+
* @param string $doc The whole unformatted PHPDoc block
160+
*
161+
* @return string
162+
*/
118163
private function getDocBody(string $doc)
119164
{
120165
$lines = explode("\n", $doc);
@@ -136,6 +181,13 @@ private function getDocBody(string $doc)
136181
return $lines;
137182
}
138183

184+
/**
185+
* Fixing the idention of the body.
186+
*
187+
* @param string $doc The PHPDoc body without the opening tags
188+
*
189+
* @return string
190+
*/
139191
private function normalizeDocBody(string $doc)
140192
{
141193
$lines = explode("\n", $doc);
@@ -159,7 +211,7 @@ private function normalizeDocBody(string $doc)
159211
}
160212

161213
$ident += $c;
162-
if($ident < 0) {
214+
if ($ident < 0) {
163215
//TODO warning - possible open-close tag mismatch
164216
$ident = 0;
165217
}
@@ -168,24 +220,39 @@ private function normalizeDocBody(string $doc)
168220
return implode("\n", $lines);
169221
}
170222

171-
private function formatDocBody(string $doc, int $ident)
223+
/**
224+
* Reconstruct the the body (adding opening and closing tag with correct idention).
225+
*
226+
* @param string $doc The PHPDoc body without the opening tags
227+
* @param string $ident The opening tag's ident
228+
*
229+
* @return string The reconstructed PHPDoc
230+
*/
231+
private function reconstructDocBody(string $doc, string $ident)
172232
{
173233
$newLine = $this->config->getNewLine();
174234
$lines = explode("\n", $doc);
175235

176236
foreach ($lines as $key => &$line) {
177237
$line = trim($line, "\r");
178-
$line = str_repeat(' ', $ident).' * '.$line;
238+
$line = $ident.' * '.$line;
179239
$line = rtrim($line);
180240
}
181241

182242
$doc = implode($newLine, $lines);
183-
$doc = str_repeat(' ', $ident).'/**'.$newLine.$doc;
184-
$doc .= $newLine.str_repeat(' ', $ident).' */'.$newLine;
243+
$doc = $ident.'/**'.$newLine.$doc;
244+
$doc .= $newLine.$ident.' */'.$newLine;
185245

186246
return $doc;
187247
}
188248

249+
/**
250+
* Finds all the PHPDoc blocks in the file.
251+
*
252+
* @param string $file File content
253+
*
254+
* @return string[] Array of PHPDoc blocks
255+
*/
189256
private function findAllDocDomment(string $file)
190257
{
191258
$regex = "#[\h]*/\*\*[\s]?([\s]*\*[^\n]*[\s]?)+[\h]*\*/[\s]?#";

0 commit comments

Comments
 (0)