Skip to content

Commit 3c5be55

Browse files
committed
Start using Timing class in more places
... which allows for removing some duplicate code and streamlining consistent display of timing information.
1 parent 55156d2 commit 3c5be55

File tree

6 files changed

+12
-41
lines changed

6 files changed

+12
-41
lines changed

.github/workflows/build-phar.yml

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ on:
1616
- 'src/Exceptions/RuntimeException.php'
1717
- 'src/Exceptions/TokenizerException.php'
1818
- 'src/Tokenizers/PHP.php'
19+
- 'src/Util/Timing.php'
1920
- 'src/Util/Tokens.php'
2021
pull_request:
2122
paths:
@@ -27,6 +28,7 @@ on:
2728
- 'src/Exceptions/RuntimeException.php'
2829
- 'src/Exceptions/TokenizerException.php'
2930
- 'src/Tokenizers/PHP.php'
31+
- 'src/Util/Timing.php'
3032
- 'src/Util/Tokens.php'
3133

3234
# Allow manually triggering the workflow.

scripts/build-phar.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHP_CodeSniffer\Exceptions\RuntimeException;
1818
use PHP_CodeSniffer\Exceptions\TokenizerException;
1919
use PHP_CodeSniffer\Tokenizers\PHP;
20+
use PHP_CodeSniffer\Util\Timing;
2021
use PHP_CodeSniffer\Util\Tokens;
2122

2223
error_reporting(E_ALL);
@@ -78,7 +79,7 @@ function stripWhitespaceAndComments($fullpath, $config)
7879
}//end stripWhitespaceAndComments()
7980

8081

81-
$startTime = microtime(true);
82+
Timing::startTiming();
8283

8384
$scripts = [
8485
'phpcs',
@@ -170,14 +171,7 @@ function stripWhitespaceAndComments($fullpath, $config)
170171
echo 'done'.PHP_EOL;
171172
}//end foreach
172173

173-
$timeTaken = ((microtime(true) - $startTime) * 1000);
174-
if ($timeTaken < 1000) {
175-
$timeTaken = round($timeTaken);
176-
echo "DONE in {$timeTaken}ms".PHP_EOL;
177-
} else {
178-
$timeTaken = round(($timeTaken / 1000), 2);
179-
echo "DONE in $timeTaken secs".PHP_EOL;
180-
}
174+
Timing::printRunTime();
181175

182176
echo PHP_EOL;
183177
echo 'Filesize generated phpcs.phar file: '.filesize(dirname(__DIR__).'/phpcs.phar').' bytes'.PHP_EOL;

src/Reports/Cbf.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use PHP_CodeSniffer\Exceptions\DeepExitException;
1717
use PHP_CodeSniffer\Files\File;
18+
use PHP_CodeSniffer\Util\Timing;
1819
use PHP_CodeSniffer\Util\Writers\StatusWriter;
1920

2021
class Cbf implements Report
@@ -73,14 +74,7 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
7374
StatusWriter::forceWrite('DONE', 0, 0);
7475
}
7576

76-
$timeTaken = ((microtime(true) - $startTime) * 1000);
77-
if ($timeTaken < 1000) {
78-
$timeTaken = round($timeTaken);
79-
StatusWriter::forceWrite(" in {$timeTaken}ms");
80-
} else {
81-
$timeTaken = round(($timeTaken / 1000), 2);
82-
StatusWriter::forceWrite(" in $timeTaken secs");
83-
}
77+
StatusWriter::forceWrite(' in '.Timing::getHumanReadableDuration(Timing::getDurationSince($startTime)));
8478
}
8579

8680
if ($fixed === true) {

src/Reports/Code.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Exception;
1313
use PHP_CodeSniffer\Files\File;
1414
use PHP_CodeSniffer\Util\Common;
15+
use PHP_CodeSniffer\Util\Timing;
1516
use PHP_CodeSniffer\Util\Writers\StatusWriter;
1617

1718
class Code implements Report
@@ -65,14 +66,7 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
6566
}
6667

6768
if (PHP_CODESNIFFER_VERBOSITY === 1) {
68-
$timeTaken = ((microtime(true) - $startTime) * 1000);
69-
if ($timeTaken < 1000) {
70-
$timeTaken = round($timeTaken);
71-
StatusWriter::forceWrite("DONE in {$timeTaken}ms");
72-
} else {
73-
$timeTaken = round(($timeTaken / 1000), 2);
74-
StatusWriter::forceWrite("DONE in $timeTaken secs");
75-
}
69+
StatusWriter::forceWrite('DONE in '.Timing::getHumanReadableDuration(Timing::getDurationSince($startTime)));
7670
}
7771

7872
$tokens = $phpcsFile->getTokens();

src/Reports/Diff.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PHP_CodeSniffer\Reports;
1111

1212
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Util\Timing;
1314
use PHP_CodeSniffer\Util\Writers\StatusWriter;
1415

1516
class Diff implements Report
@@ -51,14 +52,7 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
5152
$phpcsFile->parse();
5253

5354
if (PHP_CODESNIFFER_VERBOSITY === 1) {
54-
$timeTaken = ((microtime(true) - $startTime) * 1000);
55-
if ($timeTaken < 1000) {
56-
$timeTaken = round($timeTaken);
57-
StatusWriter::write("DONE in {$timeTaken}ms");
58-
} else {
59-
$timeTaken = round(($timeTaken / 1000), 2);
60-
StatusWriter::write("DONE in $timeTaken secs");
61-
}
55+
StatusWriter::write('DONE in '.Timing::getHumanReadableDuration(Timing::getDurationSince($startTime)));
6256
}
6357

6458
$phpcsFile->fixer->startFile($phpcsFile);

src/Runner.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -612,14 +612,7 @@ public function processFile($file)
612612
$file->process();
613613

614614
if (PHP_CODESNIFFER_VERBOSITY > 0) {
615-
$timeTaken = ((microtime(true) - $startTime) * 1000);
616-
if ($timeTaken < 1000) {
617-
$timeTaken = round($timeTaken);
618-
StatusWriter::write("DONE in {$timeTaken}ms", 0, 0);
619-
} else {
620-
$timeTaken = round(($timeTaken / 1000), 2);
621-
StatusWriter::write("DONE in $timeTaken secs", 0, 0);
622-
}
615+
StatusWriter::write('DONE in '.Timing::getHumanReadableDuration(Timing::getDurationSince($startTime)), 0, 0);
623616

624617
if (PHP_CODESNIFFER_CBF === true) {
625618
$errors = $file->getFixableCount();

0 commit comments

Comments
 (0)