GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( a92669...1312b5 )
by Ayesh
01:23
created

Timer::start()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Ayesh\PHP_Timer;
5
6
/**
7
 * Class Timer
8
 *
9
 * Helper class to measure the execution time between two points in a single
10
 * request.
11
 *
12
 * @package Ayesh\PHP_Timer
13
 */
14
class Timer {
15
  public const FORMAT_PRECISE = FALSE;
16
  public const FORMAT_MILLISECONDS = 'ms';
17
  public const FORMAT_SECONDS = 's';
18
  public const FORMAT_HUMAN = 'h';
19
20
  /**
21
   * Stores all the timers statically.
22
   * @var Stopwatch[]
23
   */
24
  static private $timers = [];
25
26
  /**
27
   * Start or resume the timer.
28
   *
29
   * Call this method to start the timer with a given key. The default key
30
   * is "default", and used in @see \Ayesh\PHP_Timer\Timer::read() and reset()
31
   * methods as well
32
   *
33
   * Calling this with the same $key will not restart the timer if it has already
34
   * started.
35
   *
36
   * @param string $key
37
   */
38
  public static function start(string $key = 'default'): void {
39
    if (isset(self::$timers[$key])) {
40
      self::$timers[$key]->start();
41
    }
42
    else {
43
      self::$timers[$key] = new Stopwatch();
44
    }
45
  }
46
47
  /**
48
   * Resets a specific timer, or default timer if not passed the $key parameter.
49
   * To reset all timers, call @see \Ayesh\PHP_Timer\Timer::resetAll().
50
   * @param string $key
51
   */
52
  public static function reset(string $key = 'default'): void {
53
    unset(self::$timers[$key]);
54
  }
55
56
  /**
57
   * Resets ALL timers.
58
   * To reset a specific timer, @see \Ayesh\PHP_Timer\Timer::reset().
59
   */
60
  public static function resetAll(): void {
61
    self::$timers = [];
62
  }
63
64
  /**
65
   * Formats the given time the processor into the given format.
66
   * @param $value
67
   * @param $format
68
   * @return string
69
   */
70
  private static function formatTime($value, $format): string {
71
    switch ($format) {
72
73
      case static::FORMAT_PRECISE:
74
        return (string) ($value * 1000);
75
76
      case static::FORMAT_MILLISECONDS:
77
        return (string) round($value * 1000, 2);
78
79
      case static::FORMAT_SECONDS:
80
        return (string) round($value, 3);
81
82
      default:
83
        return (string) ($value * 1000);
84
    }
85
  }
86
87
  /**
88
   * Returns the time elapsed in the format requested in the $format parameter.
89
   * To access a specific timer, pass the same key that
90
   * @see \Ayesh\PHP_Timer\Timer::start() was called with. If the timer was not
91
   * started, a \LogicException will be thrown.
92
   *
93
   * The default format is milliseconds. See the class constants for additional
94
   * formats.
95
   *
96
   * @param string $key The key that the timer was started with. Default value is
97
   *   "default" throughout the class.
98
   * @param string $format
99
   * @return mixed The formatted time.
100
   * @throws \LogicException
101
   */
102
  public static function read(string $key = 'default', $format = self::FORMAT_MILLISECONDS) {
103
    if (isset(self::$timers[$key])) {
104
      return self::formatTime(self::$timers[$key]->read(), $format);
105
    }
106
    throw new \LogicException('Reading timer when the given key timer was not initialized.');
107
  }
108
109
  /**
110
   * Stops the timer with the given key. Default key is "default"
111
   *
112
   * @param string $key
113
   *
114
   * @throws \LogicException If the attempted timer has not started already.
115
   */
116
  public static function stop($key = 'default'): void {
117
    if (isset(self::$timers[$key])) {
118
      self::$timers[$key]->stop();
119
    } else {
120
      throw new \LogicException('Stopping timer when the given key timer was not initialized.');
121
    }
122
  }
123
124
  /**
125
   * Return a list of timer names. Note that resetting a timer removes the timer.
126
   * @return string[]
127
   */
128
  public static function getTimers(): array {
129
    return array_keys(static::$timers);
0 ignored issues
show
Bug introduced by
Since $timers is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $timers to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
130
  }
131
}
132