Skip to content

Commit 278a3bc

Browse files
committed
Code style updates
1 parent c6a5291 commit 278a3bc

File tree

6 files changed

+64
-45
lines changed

6 files changed

+64
-45
lines changed

src/Stopwatch.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,45 @@
11
<?php
2-
declare(strict_types = 1);
2+
declare(strict_types=1);
33

44
namespace Ayesh\PHP_Timer;
55

66
class Stopwatch {
7+
78
private $accrued = 0;
9+
810
private $timestamp;
11+
912
private $running = false;
1013

1114
public function __construct() {
1215
$this->start();
1316
}
1417

15-
private function getTimestamp(): float {
16-
return microtime(true);
17-
}
18-
19-
private function accrue(): void {
20-
$this->accrued = $this->read();
21-
}
22-
2318
public function start(): void {
2419
if ($this->running) {
2520
return;
2621
}
2722

2823
$this->accrue();
29-
$this->running = true;
24+
$this->running = true;
3025
$this->timestamp = $this->getTimestamp();
3126
}
3227

28+
private function accrue(): void {
29+
$this->accrued = $this->read();
30+
}
31+
3332
public function read(): float {
3433
if ($this->running) {
3534
return $this->accrued + ($this->getTimestamp() - $this->timestamp);
3635
}
3736
return $this->accrued;
3837
}
3938

39+
private function getTimestamp(): float {
40+
return microtime(true);
41+
}
42+
4043
public function stop(): float {
4144
$this->accrue();
4245
$this->running = false;

src/Timer.php

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
declare(strict_types = 1);
2+
declare(strict_types=1);
33

44
namespace Ayesh\PHP_Timer;
55

@@ -12,9 +12,13 @@
1212
* @package Ayesh\PHP_Timer
1313
*/
1414
class Timer {
15-
public const FORMAT_PRECISE = FALSE;
15+
16+
public const FORMAT_PRECISE = false;
17+
1618
public const FORMAT_MILLISECONDS = 'ms';
19+
1720
public const FORMAT_SECONDS = 's';
21+
1822
public const FORMAT_HUMAN = 'h';
1923

2024
private const TIMES = [
@@ -25,6 +29,7 @@ class Timer {
2529

2630
/**
2731
* Stores all the timers statically.
32+
*
2833
* @var Stopwatch[]
2934
*/
3035
static private $timers = [];
@@ -33,13 +38,14 @@ class Timer {
3338
* Start or resume the timer.
3439
*
3540
* Call this method to start the timer with a given key. The default key
36-
* is "default", and used in @see \Ayesh\PHP_Timer\Timer::read() and reset()
41+
* is "default", and used in @param string $key
42+
*
43+
* @see \Ayesh\PHP_Timer\Timer::read() and reset()
3744
* methods as well
3845
*
3946
* Calling this with the same $key will not restart the timer if it has already
4047
* started.
4148
*
42-
* @param string $key
4349
*/
4450
public static function start(string $key = 'default'): void {
4551
if (isset(self::$timers[$key])) {
@@ -52,8 +58,9 @@ public static function start(string $key = 'default'): void {
5258

5359
/**
5460
* Resets a specific timer, or default timer if not passed the $key parameter.
55-
* To reset all timers, call @see \Ayesh\PHP_Timer\Timer::resetAll().
56-
* @param string $key
61+
* To reset all timers, call @param string $key
62+
*
63+
* @see \Ayesh\PHP_Timer\Timer::resetAll().
5764
*/
5865
public static function reset(string $key = 'default'): void {
5966
unset(self::$timers[$key]);
@@ -67,10 +74,36 @@ public static function resetAll(): void {
6774
self::$timers = [];
6875
}
6976

77+
/**
78+
* Returns the time elapsed in the format requested in the $format parameter.
79+
* To access a specific timer, pass the same key that
80+
*
81+
* @param string $key The key that the timer was started with. Default value is
82+
* "default" throughout the class.
83+
* @param string $format
84+
*
85+
* @return mixed The formatted time.
86+
* @throws \LogicException
87+
* @see \Ayesh\PHP_Timer\Timer::start() was called with. If the timer was not
88+
* started, a \LogicException will be thrown.
89+
*
90+
* The default format is milliseconds. See the class constants for additional
91+
* formats.
92+
*
93+
*/
94+
public static function read(string $key = 'default', $format = self::FORMAT_MILLISECONDS) {
95+
if (isset(self::$timers[$key])) {
96+
return self::formatTime(self::$timers[$key]->read(), $format);
97+
}
98+
throw new \LogicException('Reading timer when the given key timer was not initialized.');
99+
}
100+
70101
/**
71102
* Formats the given time the processor into the given format.
103+
*
72104
* @param float $value
73105
* @param string|bool $format
106+
*
74107
* @return string
75108
*/
76109
private static function formatTime(float $value, $format): string {
@@ -103,28 +136,6 @@ private static function secondsToTimeString(float $time): string {
103136
return $ms . ' ms';
104137
}
105138

106-
/**
107-
* Returns the time elapsed in the format requested in the $format parameter.
108-
* To access a specific timer, pass the same key that
109-
* @see \Ayesh\PHP_Timer\Timer::start() was called with. If the timer was not
110-
* started, a \LogicException will be thrown.
111-
*
112-
* The default format is milliseconds. See the class constants for additional
113-
* formats.
114-
*
115-
* @param string $key The key that the timer was started with. Default value is
116-
* "default" throughout the class.
117-
* @param string $format
118-
* @return mixed The formatted time.
119-
* @throws \LogicException
120-
*/
121-
public static function read(string $key = 'default', $format = self::FORMAT_MILLISECONDS) {
122-
if (isset(self::$timers[$key])) {
123-
return self::formatTime(self::$timers[$key]->read(), $format);
124-
}
125-
throw new \LogicException('Reading timer when the given key timer was not initialized.');
126-
}
127-
128139
/**
129140
* Stops the timer with the given key. Default key is "default"
130141
*
@@ -135,13 +146,15 @@ public static function read(string $key = 'default', $format = self::FORMAT_MILL
135146
public static function stop($key = 'default'): void {
136147
if (isset(self::$timers[$key])) {
137148
self::$timers[$key]->stop();
138-
} else {
149+
}
150+
else {
139151
throw new \LogicException('Stopping timer when the given key timer was not initialized.');
140152
}
141153
}
142154

143155
/**
144156
* Return a list of timer names. Note that resetting a timer removes the timer.
157+
*
145158
* @return string[]
146159
*/
147160
public static function getTimers(): array {

tests/StopwatchTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@
99
* @group time-sensitive
1010
*/
1111
class StopwatchTest extends TestCase {
12+
1213
public function testTimerStoppedValueRetention(): void {
1314
$stopwatch = new Stopwatch();
1415
$stopwatch->stop();
1516
$time_1 = $stopwatch->read();
1617
$this->assertIsFloat($time_1);
1718
sleep(20);
1819
$time_2 = $stopwatch->read();
19-
$this->assertIsFloat( $time_2);
20+
$this->assertIsFloat($time_2);
2021
$this->assertSame($time_1, $time_2);
2122
}
2223

2324
public function testTimerContiniuous(): void {
2425
$stopwatch = new Stopwatch();
25-
$time_1 = $stopwatch->read();
26+
$time_1 = $stopwatch->read();
2627
$this->assertIsFloat($time_1);
2728
sleep(20);
2829
$time_2 = $stopwatch->read();

tests/TimerFormatterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @group time-sensitive
1111
*/
1212
class TimerFormatterTest extends TestCase {
13+
1314
public function testTimerFormat_Human(): void {
1415
Timer::start(__FUNCTION__);
1516

tests/TimerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ public function testUnsupportedKeyType(): void {
1717
Timer::start(new \stdClass());
1818
}
1919

20-
private function sleepHalfSec(int $count = 1): void {
21-
usleep(500000 * $count);
22-
}
23-
2420
public function testStopValuesRetained(): void {
2521
Timer::start(__FUNCTION__);
2622
Timer::stop(__FUNCTION__);
@@ -29,6 +25,10 @@ public function testStopValuesRetained(): void {
2925
$this->assertEquals(Timer::read(__FUNCTION__, Timer::FORMAT_PRECISE), $stopped_at);
3026
}
3127

28+
private function sleepHalfSec(int $count = 1): void {
29+
usleep(500000 * $count);
30+
}
31+
3232
public function testUnstoppedValuesContinue(): void {
3333
Timer::start(__FUNCTION__);
3434
$stopped_at = Timer::read(__FUNCTION__);

tests/TimerTest2.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @group time-sensitive
1111
*/
1212
class TimerTest2 extends TestCase {
13+
1314
public function testTimerDefaultRunning() {
1415
Timer::start();
1516
sleep(20);

0 commit comments

Comments
 (0)