Skip to content

Commit 0a3126d

Browse files
committed
Optimized inline func calls
1 parent e0a3f19 commit 0a3126d

File tree

4 files changed

+58
-58
lines changed

4 files changed

+58
-58
lines changed

src/Enum.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ final public function getName()
120120
if ($this->ordinal !== null) {
121121
return self::$names[static::class][$this->ordinal];
122122
}
123-
return array_search($this->value, self::detectConstants(static::class), true);
123+
return \array_search($this->value, self::detectConstants(static::class), true);
124124
}
125125

126126
/**
@@ -160,7 +160,7 @@ final public function is($enumerator)
160160

161161
// The following additional conditions are required only because of the issue of serializable singletons
162162
|| ($enumerator instanceof static
163-
&& get_class($enumerator) === static::class
163+
&& \get_class($enumerator) === static::class
164164
&& $enumerator->value === $this->value
165165
);
166166
}
@@ -175,7 +175,7 @@ final public function is($enumerator)
175175
*/
176176
final public static function get($value)
177177
{
178-
if ($value instanceof static && get_class($value) === static::class) {
178+
if ($value instanceof static && \get_class($value) === static::class) {
179179
return $value;
180180
}
181181

@@ -194,11 +194,11 @@ final public static function byValue($value)
194194
{
195195
$class = static::class;
196196
$constants = self::detectConstants($class);
197-
$name = array_search($value, $constants, true);
197+
$name = \array_search($value, $constants, true);
198198
if ($name === false) {
199-
$message = is_scalar($value)
200-
? 'Unknown value ' . var_export($value, true)
201-
: 'Invalid value of type ' . (is_object($value) ? get_class($value) : gettype($value));
199+
$message = \is_scalar($value)
200+
? 'Unknown value ' . \var_export($value, true)
201+
: 'Invalid value of type ' . (\is_object($value) ? \get_class($value) : \gettype($value));
202202
throw new InvalidArgumentException($message);
203203
}
204204

@@ -226,11 +226,11 @@ final public static function byName($name)
226226
}
227227

228228
$const = $class . '::' . $name;
229-
if (!defined($const)) {
229+
if (!\defined($const)) {
230230
throw new InvalidArgumentException($const . ' not defined');
231231
}
232232

233-
return self::$instances[$class][$name] = new $class(constant($const));
233+
return self::$instances[$class][$name] = new $class(\constant($const));
234234
}
235235

236236
/**
@@ -253,7 +253,7 @@ final public static function byOrdinal($ordinal)
253253
if (!isset(self::$names[$class][$ordinal])) {
254254
throw new InvalidArgumentException(sprintf(
255255
'Invalid ordinal number, must between 0 and %s',
256-
count(self::$names[$class]) - 1
256+
\count(self::$names[$class]) - 1
257257
));
258258
}
259259

@@ -263,7 +263,7 @@ final public static function byOrdinal($ordinal)
263263
}
264264

265265
$const = $class . '::' . $name;
266-
return self::$instances[$class][$name] = new $class(constant($const));
266+
return self::$instances[$class][$name] = new $class(\constant($const));
267267
}
268268

269269
/**
@@ -276,7 +276,7 @@ final public static function getEnumerators()
276276
if (!isset(self::$names[static::class])) {
277277
self::detectConstants(static::class);
278278
}
279-
return array_map([static::class, 'byName'], self::$names[static::class]);
279+
return \array_map([static::class, 'byName'], self::$names[static::class]);
280280
}
281281

282282
/**
@@ -286,7 +286,7 @@ final public static function getEnumerators()
286286
*/
287287
final public static function getValues()
288288
{
289-
return array_values(self::detectConstants(static::class));
289+
return \array_values(self::detectConstants(static::class));
290290
}
291291

292292
/**
@@ -308,8 +308,8 @@ final public static function getNames()
308308
*/
309309
final public static function getOrdinals()
310310
{
311-
$count = count(self::detectConstants(static::class));
312-
return $count === 0 ? array() : range(0, $count - 1);
311+
$count = \count(self::detectConstants(static::class));
312+
return $count === 0 ? array() : \range(0, $count - 1);
313313
}
314314

315315
/**
@@ -331,12 +331,12 @@ final public static function getConstants()
331331
*/
332332
final public static function has($value)
333333
{
334-
if ($value instanceof static && get_class($value) === static::class) {
334+
if ($value instanceof static && \get_class($value) === static::class) {
335335
return true;
336336
}
337337

338338
$constants = self::detectConstants(static::class);
339-
return in_array($value, $constants, true);
339+
return \in_array($value, $constants, true);
340340
}
341341

342342
/**
@@ -373,22 +373,22 @@ private static function detectConstants($class)
373373
// Detect ambiguous values and report names
374374
$ambiguous = array();
375375
foreach ($constants as $value) {
376-
$names = array_keys($constants, $value, true);
377-
if (count($names) > 1) {
378-
$ambiguous[var_export($value, true)] = $names;
376+
$names = \array_keys($constants, $value, true);
377+
if (\count($names) > 1) {
378+
$ambiguous[\var_export($value, true)] = $names;
379379
}
380380
}
381381
if (!empty($ambiguous)) {
382382
throw new LogicException(
383383
'All possible values needs to be unique. The following are ambiguous: '
384-
. implode(', ', array_map(function ($names) use ($constants) {
385-
return implode('/', $names) . '=' . var_export($constants[$names[0]], true);
384+
. \implode(', ', \array_map(function ($names) use ($constants) {
385+
return \implode('/', $names) . '=' . \var_export($constants[$names[0]], true);
386386
}, $ambiguous))
387387
);
388388
}
389389

390390
self::$constants[$class] = $constants;
391-
self::$names[$class] = array_keys($constants);
391+
self::$names[$class] = \array_keys($constants);
392392
}
393393

394394
return self::$constants[$class];

src/EnumMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class EnumMap extends SplObjectStorage
2828
*/
2929
public function __construct($enumeration)
3030
{
31-
if (!is_subclass_of($enumeration, Enum::class)) {
31+
if (!\is_subclass_of($enumeration, Enum::class)) {
3232
throw new InvalidArgumentException(sprintf(
3333
"This EnumMap can handle subclasses of '%s' only",
3434
Enum::class

src/EnumSerializableTrait.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ abstract public function getValue();
3333
*/
3434
public function serialize()
3535
{
36-
return serialize($this->getValue());
36+
return \serialize($this->getValue());
3737
}
3838

3939
/**
@@ -45,17 +45,17 @@ public function serialize()
4545
*/
4646
public function unserialize($serialized)
4747
{
48-
$value = unserialize($serialized);
48+
$value = \unserialize($serialized);
4949
$constants = self::getConstants();
50-
$name = array_search($value, $constants, true);
50+
$name = \array_search($value, $constants, true);
5151
if ($name === false) {
52-
$message = is_scalar($value)
53-
? 'Unknown value ' . var_export($value, true)
54-
: 'Invalid value of type ' . (is_object($value) ? get_class($value) : gettype($value));
52+
$message = \is_scalar($value)
53+
? 'Unknown value ' . \var_export($value, true)
54+
: 'Invalid value of type ' . (\is_object($value) ? \get_class($value) : \gettype($value));
5555
throw new RuntimeException($message);
5656
}
5757

58-
$class = get_class($this);
58+
$class = \get_class($this);
5959
$enumerator = $this;
6060
$closure = function () use ($class, $name, $value, $enumerator) {
6161
if ($this->value !== null && $value !== null) {
@@ -68,7 +68,7 @@ public function unserialize($serialized)
6868
self::$instances[$class][$name] = $enumerator;
6969
}
7070
};
71-
$closure = $closure->bindTo($this, 'MabeEnum\Enum');
71+
$closure = $closure->bindTo($this, Enum::class);
7272
$closure();
7373
}
7474
}

src/EnumSet.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ class EnumSet implements Iterator, Countable
6565
*/
6666
public function __construct($enumeration)
6767
{
68-
if (!is_subclass_of($enumeration, Enum::class)) {
69-
throw new InvalidArgumentException(sprintf(
68+
if (!\is_subclass_of($enumeration, Enum::class)) {
69+
throw new InvalidArgumentException(\sprintf(
7070
"%s can handle subclasses of '%s' only",
7171
static::class,
7272
Enum::class
@@ -81,7 +81,7 @@ public function __construct($enumeration)
8181
// we will switch this into a binary bitset
8282
if ($this->ordinalMax > \PHP_INT_SIZE * 8) {
8383
// init binary bitset with zeros
84-
$this->bitset = str_repeat("\0", ceil($this->ordinalMax / 8));
84+
$this->bitset = \str_repeat("\0", ceil($this->ordinalMax / 8));
8585

8686
// switch internal binary bitset functions
8787
$this->fnDoRewind = 'doRewindBin';
@@ -203,7 +203,7 @@ public function rewind()
203203
*/
204204
private function doRewindBin()
205205
{
206-
if (ltrim($this->bitset, "\0") !== '') {
206+
if (\ltrim($this->bitset, "\0") !== '') {
207207
$this->ordinal = -1;
208208
$this->next();
209209
} else {
@@ -267,14 +267,14 @@ private function doCountBin()
267267
{
268268
$count = 0;
269269
$bitset = $this->bitset;
270-
$byteLen = strlen($bitset);
270+
$byteLen = \strlen($bitset);
271271
for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) {
272272
if ($bitset[$bytePos] === "\0") {
273273
// fast skip null byte
274274
continue;
275275
}
276276

277-
$ord = ord($bitset[$bytePos]);
277+
$ord = \ord($bitset[$bytePos]);
278278
for ($bitPos = 0; $bitPos < 8; ++$bitPos) {
279279
if ($ord & (1 << $bitPos)) {
280280
++$count;
@@ -470,14 +470,14 @@ private function doGetOrdinalsBin()
470470
{
471471
$ordinals = [];
472472
$bitset = $this->bitset;
473-
$byteLen = strlen($bitset);
473+
$byteLen = \strlen($bitset);
474474
for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) {
475475
if ($bitset[$bytePos] === "\0") {
476476
// fast skip null byte
477477
continue;
478478
}
479479

480-
$ord = ord($bitset[$bytePos]);
480+
$ord = \ord($bitset[$bytePos]);
481481
for ($bitPos = 0; $bitPos < 8; ++$bitPos) {
482482
if ($ord & (1 << $bitPos)) {
483483
$ordinals[] = $bytePos * 8 + $bitPos;
@@ -582,8 +582,8 @@ private function doGetBinaryBitsetLeBin()
582582
*/
583583
private function doGetBinaryBitsetLeInt()
584584
{
585-
$bin = pack(\PHP_INT_SIZE === 8 ? 'P' : 'V', $this->bitset);
586-
return substr($bin, 0, ceil($this->ordinalMax / 8));
585+
$bin = \pack(\PHP_INT_SIZE === 8 ? 'P' : 'V', $this->bitset);
586+
return \substr($bin, 0, \ceil($this->ordinalMax / 8));
587587
}
588588

589589
/**
@@ -597,7 +597,7 @@ private function doGetBinaryBitsetLeInt()
597597
*/
598598
public function setBinaryBitsetLe($bitset)
599599
{
600-
if (!is_string($bitset)) {
600+
if (!\is_string($bitset)) {
601601
throw new InvalidArgumentException('Bitset must be a string');
602602
}
603603

@@ -618,29 +618,29 @@ public function setBinaryBitsetLe($bitset)
618618
*/
619619
private function doSetBinaryBitsetLeBin($bitset)
620620
{
621-
$size = strlen($this->bitset);
622-
$sizeIn = strlen($bitset);
621+
$size = \strlen($this->bitset);
622+
$sizeIn = \strlen($bitset);
623623

624624
if ($sizeIn < $size) {
625625
// add "\0" if the given bitset is not long enough
626-
$bitset .= str_repeat("\0", $size - $sizeIn);
626+
$bitset .= \str_repeat("\0", $size - $sizeIn);
627627
} elseif ($sizeIn > $size) {
628-
if (ltrim(substr($bitset, $size), "\0") !== '') {
628+
if (\ltrim(\substr($bitset, $size), "\0") !== '') {
629629
throw new InvalidArgumentException('Out-Of-Range bits detected');
630630
}
631-
$bitset = substr($bitset, 0, $size);
631+
$bitset = \substr($bitset, 0, $size);
632632
}
633633

634634
// truncate out-of-range bits of last byte
635635
$lastByteMaxOrd = $this->ordinalMax % 8;
636636
if ($lastByteMaxOrd !== 0) {
637637
$lastByte = $bitset[$size - 1];
638-
$lastByteExpected = chr((1 << $lastByteMaxOrd) - 1) & $lastByte;
638+
$lastByteExpected = \chr((1 << $lastByteMaxOrd) - 1) & $lastByte;
639639
if ($lastByte !== $lastByteExpected) {
640640
throw new InvalidArgumentException('Out-Of-Range bits detected');
641641
}
642642

643-
$this->bitset = substr($bitset, 0, -1) . $lastByteExpected;
643+
$this->bitset = \substr($bitset, 0, -1) . $lastByteExpected;
644644
}
645645

646646
$this->bitset = $bitset;
@@ -657,10 +657,10 @@ private function doSetBinaryBitsetLeBin($bitset)
657657
*/
658658
private function doSetBinaryBitsetLeInt($bitset)
659659
{
660-
$len = strlen($bitset);
660+
$len = \strlen($bitset);
661661
$int = 0;
662662
for ($i = 0; $i < $len; ++$i) {
663-
$ord = ord($bitset[$i]);
663+
$ord = \ord($bitset[$i]);
664664

665665
if ($ord && $i > \PHP_INT_SIZE - 1) {
666666
throw new InvalidArgumentException('Out-Of-Range bits detected');
@@ -683,7 +683,7 @@ private function doSetBinaryBitsetLeInt($bitset)
683683
*/
684684
public function getBinaryBitsetBe()
685685
{
686-
return strrev($this->bitset);
686+
return \strrev($this->bitset);
687687
}
688688

689689
/**
@@ -697,10 +697,10 @@ public function getBinaryBitsetBe()
697697
*/
698698
public function setBinaryBitsetBe($bitset)
699699
{
700-
if (!is_string($bitset)) {
700+
if (!\is_string($bitset)) {
701701
throw new InvalidArgumentException('Bitset must be a string');
702702
}
703-
$this->setBinaryBitsetLe(strrev($bitset));
703+
$this->setBinaryBitsetLe(\strrev($bitset));
704704
}
705705

706706
/**
@@ -730,7 +730,7 @@ public function getBit($ordinal)
730730
*/
731731
private function doGetBitBin($ordinal)
732732
{
733-
return (ord($this->bitset[(int) ($ordinal / 8)]) & 1 << ($ordinal % 8)) !== 0;
733+
return (\ord($this->bitset[(int) ($ordinal / 8)]) & 1 << ($ordinal % 8)) !== 0;
734734
}
735735

736736
/**
@@ -785,7 +785,7 @@ public function setBit($ordinal, $bit)
785785
private function doSetBitBin($ordinal)
786786
{
787787
$byte = (int) ($ordinal / 8);
788-
$this->bitset[$byte] = $this->bitset[$byte] | chr(1 << ($ordinal % 8));
788+
$this->bitset[$byte] = $this->bitset[$byte] | \chr(1 << ($ordinal % 8));
789789
}
790790

791791
/**
@@ -816,7 +816,7 @@ private function doSetBitInt($ordinal)
816816
private function doUnsetBitBin($ordinal)
817817
{
818818
$byte = (int) ($ordinal / 8);
819-
$this->bitset[$byte] = $this->bitset[$byte] & chr(~(1 << ($ordinal % 8)));
819+
$this->bitset[$byte] = $this->bitset[$byte] & \chr(~(1 << ($ordinal % 8)));
820820
}
821821

822822
/**

0 commit comments

Comments
 (0)