Skip to content

Commit 292322b

Browse files
authored
Merge pull request #1074 from PHPCSStandards/phpcs-4.0/feature/use-timing-class-in-more-places
Start using Timing class in more places
2 parents c1b1f39 + 3c5be55 commit 292322b

File tree

8 files changed

+55
-43
lines changed

8 files changed

+55
-43
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: '.number_format(filesize(dirname(__DIR__).'/phpcs.phar'), 0, ',', '.').' 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();

src/Util/Timing.php

+25-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@
22
/**
33
* Timing functions for the run.
44
*
5+
* ---------------------------------------------------------------------------------------------
6+
* This class is intended for internal use only and is not part of the public API.
7+
* This also means that it has no promise of backward compatibility. Use at your own risk.
8+
* ---------------------------------------------------------------------------------------------
9+
*
10+
* @internal
11+
*
512
* @author Greg Sherwood <gsherwood@squiz.net>
13+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
614
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
15+
* @copyright 2025 PHPCSStandards and contributors
716
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
817
*/
918

1019
namespace PHP_CodeSniffer\Util;
1120

1221
use PHP_CodeSniffer\Util\Writers\StatusWriter;
1322

14-
class Timing
23+
final class Timing
1524
{
1625

1726
/**
@@ -29,7 +38,7 @@ class Timing
2938
private const SECOND_IN_MS = 1000;
3039

3140
/**
32-
* The start time of the run in microseconds.
41+
* The start time of the run in seconds.
3342
*
3443
* @var float
3544
*/
@@ -73,6 +82,20 @@ public static function getDuration()
7382
}//end getDuration()
7483

7584

85+
/**
86+
* Get the duration since a given start time up to "now".
87+
*
88+
* @param float $startTime Start time in microseconds.
89+
*
90+
* @return float Duration in milliseconds.
91+
*/
92+
public static function getDurationSince($startTime)
93+
{
94+
return ((microtime(true) - $startTime) * 1000);
95+
96+
}//end getDurationSince()
97+
98+
7699
/**
77100
* Convert a duration in milliseconds to a human readable duration string.
78101
*

tests/Core/Util/Timing/TimingTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ public function testGetDurationWithStartReturnsMilliseconds()
6060
}//end testGetDurationWithStartReturnsMilliseconds()
6161

6262

63+
/**
64+
* Verify that getDurationSince() returns the time in milliseconds.
65+
*
66+
* @return void
67+
*/
68+
public function testGetDurationSinceReturnsMilliseconds()
69+
{
70+
$startTime = microtime(true);
71+
usleep(1500);
72+
$duration = Timing::getDurationSince($startTime);
73+
74+
$this->assertIsFloat($duration);
75+
$this->assertGreaterThan(1, $duration);
76+
$this->assertLessThan(15, $duration);
77+
78+
}//end testGetDurationSinceReturnsMilliseconds()
79+
80+
6381
/**
6482
* Verify that printRunTime() doesn't print anything if the timer wasn't started.
6583
*

0 commit comments

Comments
 (0)