Skip to content

Commit

Permalink
Added backslash to PHP internal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Nov 2, 2015
1 parent a20560c commit 8a135c5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 35 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
}
],
"require": {
"php": ">=5.4"
"php": ">=5.4",
"nilportugues/php_backslasher": "^0.2.1"
},
"require-dev": {
"phpunit/phpunit": "~4.2",
"fiunchinho/phpunit-randomizer": "1.0.*@dev",
"fabpot/php-cs-fixer": "dev-master"
"fabpot/php-cs-fixer": "dev-master",
"satooshi/php-coveralls": "dev-master"
},
"config": {
"bin-dir": "bin/",
Expand Down
4 changes: 2 additions & 2 deletions src/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ private static function createNamespacelessUuid()
*/
private static function generate($uuidKey, array $arguments)
{
$classAndMethod = explode('::', self::$uuidMap[$uuidKey]);
$classAndMethod = \explode('::', self::$uuidMap[$uuidKey]);

return call_user_func_array($classAndMethod, $arguments);
return \call_user_func_array($classAndMethod, $arguments);
}

/**
Expand Down
18 changes: 9 additions & 9 deletions src/Versions/AbstractUuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ abstract class AbstractUuid
protected static function uuidFromHashedName($hash, $version)
{
// Set the version number
$timeHi = hexdec(substr($hash, 12, 4)) & 0x0fff;
$timeHi = \hexdec(\substr($hash, 12, 4)) & 0x0fff;
$timeHi &= ~(0xf000);
$timeHi |= $version << 12;

// Set the variant to RFC 4122
$clockSeqHi = hexdec(substr($hash, 16, 2)) & 0x3f;
$clockSeqHi = \hexdec(\substr($hash, 16, 2)) & 0x3f;
$clockSeqHi &= ~(0xc0);
$clockSeqHi |= 0x80;

return [
self::TIME_LOW => substr($hash, 0, 8),
self::TIME_MID => substr($hash, 8, 4),
self::TIME_HI_AND_VERSION => sprintf('%04x', $timeHi),
self::CLOCK_SEQ_HI_AND_RESERVED => sprintf('%02x', $clockSeqHi),
self::CLOCK_SEQ_LOW => substr($hash, 18, 2),
self::NODE => substr($hash, 20, 12),
self::TIME_LOW => \substr($hash, 0, 8),
self::TIME_MID => \substr($hash, 8, 4),
self::TIME_HI_AND_VERSION => \sprintf('%04x', $timeHi),
self::CLOCK_SEQ_HI_AND_RESERVED => \sprintf('%02x', $clockSeqHi),
self::CLOCK_SEQ_LOW => \substr($hash, 18, 2),
self::NODE => \substr($hash, 20, 12),
];
}

Expand All @@ -63,7 +63,7 @@ protected static function uuidFromHashedName($hash, $version)
*/
protected static function toString(array $fields)
{
return vsprintf(
return \vsprintf(
'%08s-%04s-%04s-%02s%02s-%012s',
$fields
);
Expand Down
4 changes: 2 additions & 2 deletions src/Versions/Uuid4.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Uuid4 extends AbstractUuid implements UuidInterface
public static function create($namespace = null, $name = null)
{
$bytes = self::generateBytes(16);
$hex = bin2hex($bytes);
$hex = \bin2hex($bytes);

return self::toString(self::uuidFromHashedName($hex, 4));
}
Expand All @@ -43,7 +43,7 @@ private static function generateBytes($length)
{
$bytes = '';
for ($i = 1; $i <= $length; $i++) {
$bytes = chr(mt_rand(0, 255)) . $bytes;
$bytes = \chr(\mt_rand(0, 255)) . $bytes;
}

return $bytes;
Expand Down
40 changes: 20 additions & 20 deletions src/Versions/Uuid5.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function create($namespace = null, $name = null)
$fields = self::fromString($namespace);
}

$hash = sha1(self::getBytes($fields) . $name);
$hash = \sha1(self::getBytes($fields) . $name);

return self::toString(self::uuidFromHashedName($hash, 5));
}
Expand All @@ -68,39 +68,39 @@ private static function fromString($name)
{
$components = self::calculateComponents($name);

$nameParsed = implode('-', $components);
$nameParsed = \implode('-', $components);
if (!self::isValid($nameParsed)) {
throw new \InvalidArgumentException('Invalid UUID string: ' . $name);
}

return [
self::TIME_LOW => sprintf('%08s', $components[0]),
self::TIME_MID => sprintf('%04s', $components[1]),
self::TIME_HI_AND_VERSION => sprintf('%04s', $components[2]),
self::CLOCK_SEQ_HI_AND_RESERVED => sprintf('%02s', substr($components[3], 0, 2)),
self::CLOCK_SEQ_LOW => sprintf('%02s', substr($components[3], 2)),
self::NODE => sprintf('%012s', $components[4]),
self::TIME_LOW => \sprintf('%08s', $components[0]),
self::TIME_MID => \sprintf('%04s', $components[1]),
self::TIME_HI_AND_VERSION => \sprintf('%04s', $components[2]),
self::CLOCK_SEQ_HI_AND_RESERVED => \sprintf('%02s', \substr($components[3], 0, 2)),
self::CLOCK_SEQ_LOW => \sprintf('%02s', \substr($components[3], 2)),
self::NODE => \sprintf('%012s', $components[4]),
];
}

/**
* We have stripped out the dashes and are breaking up the string using
* substr(). In this way, we can accept a full hex value that doesn't
* \substr(). In this way, we can accept a full hex value that doesn't
* contain dashes.
* @param $name
*
* @return array
*/
private static function calculateComponents($name)
{
$nameParsed = str_replace(['urn:', 'uuid:', '{', '}', '-'], '', $name);
$nameParsed = \str_replace(['urn:', 'uuid:', '{', '}', '-'], '', $name);

$components = [
substr($nameParsed, 0, 8),
substr($nameParsed, 8, 4),
substr($nameParsed, 12, 4),
substr($nameParsed, 16, 4),
substr($nameParsed, 20),
\substr($nameParsed, 0, 8),
\substr($nameParsed, 8, 4),
\substr($nameParsed, 12, 4),
\substr($nameParsed, 16, 4),
\substr($nameParsed, 20),
];
return $components;
}
Expand All @@ -114,9 +114,9 @@ private static function calculateComponents($name)
*/
private static function isValid($uuid)
{
$uuid = str_replace(['urn:', 'uuid:', '{', '}'], '', $uuid);
$uuid = \str_replace(['urn:', 'uuid:', '{', '}'], '', $uuid);

if (0 == preg_match('/' . self::VALID_PATTERN . '/', $uuid)) {
if (0 == \preg_match('/' . self::VALID_PATTERN . '/', $uuid)) {
return false;
}

Expand All @@ -134,8 +134,8 @@ private static function isValid($uuid)
private static function getBytes(array $fields)
{
$bytes = '';
foreach (range(-2, -32, 2) as $step) {
$bytes = chr(hexdec(substr(self::getHex($fields), $step, 2))) . $bytes;
foreach (\range(-2, -32, 2) as $step) {
$bytes = \chr(\hexdec(\substr(self::getHex($fields), $step, 2))) . $bytes;
}

return $bytes;
Expand All @@ -150,6 +150,6 @@ private static function getBytes(array $fields)
*/
private static function getHex(array $fields)
{
return str_replace('-', '', static::toString($fields));
return \str_replace('-', '', static::toString($fields));
}
}

0 comments on commit 8a135c5

Please sign in to comment.