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 3be06d9 commit 7d3d211
Show file tree
Hide file tree
Showing 41 changed files with 97 additions and 60 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"symfony/yaml": "^2.7"
},
"require-dev": {
"phpunit/phpunit": "4.4.*",
"phpunit/phpunit": "5.*",
"nilportugues/php_backslasher": "~0.2",
"fabpot/php-cs-fixer": "^1.9",
"mmoreram/php-formatter": "dev-master",
"satooshi/php-coveralls": "dev-master",
Expand Down
1 change: 1 addition & 0 deletions src/DeepCopySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer;

use ReflectionClass;
Expand Down
1 change: 1 addition & 0 deletions src/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer;

use NilPortugues\Serializer\Strategy\JsonStrategy;
Expand Down
46 changes: 23 additions & 23 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ class Serializer
*/
public function __construct(StrategyInterface $strategy)
{
$this->isHHVM = defined('HHVM_VERSION');
$this->isHHVM = \defined('HHVM_VERSION');
if ($this->isHHVM) {
// @codeCoverageIgnoreStart
$this->serializationMap = array_merge(
$this->serializationMap = \array_merge(
$this->serializationMap,
include realpath(dirname(__FILE__).'/Mapping/serialization_hhvm.php')
include \realpath(\dirname(__FILE__).'/Mapping/serialization_hhvm.php')
);
$this->unserializationMapHHVM = include realpath(dirname(__FILE__).'/Mapping/unserialization_hhvm.php');
$this->unserializationMapHHVM = include \realpath(\dirname(__FILE__).'/Mapping/unserialization_hhvm.php');
// @codeCoverageIgnoreEnd
}
$this->serializationStrategy = $strategy;
Expand Down Expand Up @@ -142,15 +142,15 @@ protected function serializeData($value)

if ($this->isHHVM && ($value instanceof \DateTimeZone || $value instanceof \DateInterval)) {
// @codeCoverageIgnoreStart
return call_user_func_array($this->serializationMap[get_class($value)], [$this, $value]);
return \call_user_func_array($this->serializationMap[get_class($value)], [$this, $value]);
// @codeCoverageIgnoreEnd
}

if (is_object($value)) {
if (\is_object($value)) {
return $this->serializeObject($value);
}

$type = (gettype($value) && $value !== null) ? gettype($value) : 'string';
$type = (\gettype($value) && $value !== null) ? \gettype($value) : 'string';
$func = $this->serializationMap[$type];

return $this->$func($value);
Expand All @@ -173,7 +173,7 @@ protected function guardForUnsupportedValues($value)
);
}

if (is_resource($value)) {
if (\is_resource($value)) {
throw new SerializerException('Resource is not supported in Serializer');
}
}
Expand All @@ -187,7 +187,7 @@ protected function guardForUnsupportedValues($value)
*/
public function unserialize($value)
{
if (is_array($value) && isset($value[self::SCALAR_TYPE])) {
if (\is_array($value) && isset($value[self::SCALAR_TYPE])) {
return $this->unserializeData($value);
}

Expand Down Expand Up @@ -223,7 +223,7 @@ protected function unserializeData($value)
return $this->unserializeObject($value);
}

return array_map([$this, __FUNCTION__], $value);
return \array_map([$this, __FUNCTION__], $value);
}

/**
Expand All @@ -235,9 +235,9 @@ protected function getScalarValue($value)
{
switch ($value[self::SCALAR_TYPE]) {
case 'integer':
return intval($value[self::SCALAR_VALUE]);
return \intval($value[self::SCALAR_VALUE]);
case 'float':
return floatval($value[self::SCALAR_VALUE]);
return \floatval($value[self::SCALAR_VALUE]);
case 'boolean':
return $value[self::SCALAR_VALUE];
case 'NULL':
Expand Down Expand Up @@ -291,7 +291,7 @@ protected function unserializeDateTimeFamilyObject(array $value, $className)
if ($this->isDateTimeFamilyObject($className)) {
if ($this->isHHVM) {
// @codeCoverageIgnoreStart
return call_user_func_array(
return \call_user_func_array(
$this->unserializationMapHHVM[$className],
[$this, $className, $value]
);
Expand All @@ -315,7 +315,7 @@ protected function isDateTimeFamilyObject($className)
$isDateTime = false;

foreach ($this->dateTimeClassType as $class) {
$isDateTime = $isDateTime || is_subclass_of($className, $class, true) || $class === $className;
$isDateTime = $isDateTime || \is_subclass_of($className, $class, true) || $class === $className;
}

return $isDateTime;
Expand All @@ -334,13 +334,13 @@ protected function restoreUsingUnserialize($className, array $attributes)
}

$obj = (object) $attributes;
$serialized = preg_replace(
$serialized = \preg_replace(
'|^O:\d+:"\w+":|',
'O:'.strlen($className).':"'.$className.'":',
serialize($obj)
\serialize($obj)
);

return unserialize($serialized);
return \unserialize($serialized);
}

/**
Expand All @@ -357,7 +357,7 @@ protected function unserializeUserDefinedObject(array $value, $className)
$this->objectMapping[$this->objectMappingIndex++] = $obj;
$this->setUnserializedObjectProperties($value, $ref, $obj);

if (method_exists($obj, '__wakeup')) {
if (\method_exists($obj, '__wakeup')) {
$obj->__wakeup();
}

Expand Down Expand Up @@ -393,7 +393,7 @@ protected function setUnserializedObjectProperties(array $value, ReflectionClass
*/
protected function serializeScalar($value)
{
$type = gettype($value);
$type = \gettype($value);
if ($type === 'double') {
$type = 'float';
}
Expand All @@ -411,7 +411,7 @@ protected function serializeScalar($value)
*/
protected function serializeArray(array $value)
{
if (array_key_exists(self::MAP_TYPE, $value)) {
if (\array_key_exists(self::MAP_TYPE, $value)) {
return $value;
}

Expand Down Expand Up @@ -455,7 +455,7 @@ protected function serializeInternalClass($value, $className, ReflectionClass $r
{
$paramsToSerialize = $this->getObjectProperties($ref, $value);
$data = [self::CLASS_IDENTIFIER_KEY => $className];
$data += array_map([$this, 'serializeData'], $this->extractObjectData($value, $ref, $paramsToSerialize));
$data += \array_map([$this, 'serializeData'], $this->extractObjectData($value, $ref, $paramsToSerialize));

return $data;
}
Expand All @@ -475,7 +475,7 @@ protected function getObjectProperties(ReflectionClass $ref, $value)
$props[] = $prop->getName();
}

return array_unique(array_merge($props, array_keys(get_object_vars($value))));
return \array_unique(\array_merge($props, \array_keys(\get_object_vars($value))));
}

/**
Expand Down Expand Up @@ -530,7 +530,7 @@ protected function extractAllInhertitedProperties($value, ReflectionClass $rc, a
$property->setAccessible(true);
$rp[$property->getName()] = $property->getValue($this);
}
$data = array_merge($rp, $data);
$data = \array_merge($rp, $data);
} while ($rc = $rc->getParentClass());
}
}
1 change: 1 addition & 0 deletions src/Serializer/HHVM/DatePeriodSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Serializer\HHVM;

/**
Expand Down
1 change: 1 addition & 0 deletions src/Serializer/HHVM/DateTimeImmutableSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Serializer\HHVM;

use NilPortugues\Serializer\Serializer;
Expand Down
1 change: 1 addition & 0 deletions src/Serializer/HHVM/DateTimeSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Serializer\HHVM;

use NilPortugues\Serializer\Serializer;
Expand Down
7 changes: 4 additions & 3 deletions src/Serializer/InternalClasses/DateIntervalSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Serializer\InternalClasses;

use DateInterval;
Expand Down Expand Up @@ -72,10 +73,10 @@ protected static function getTypedValue(Serializer $serializer, array $value)
public static function serialize(Serializer $serializer, DateInterval $dateInterval)
{
return array(
Serializer::CLASS_IDENTIFIER_KEY => get_class($dateInterval),
Serializer::CLASS_IDENTIFIER_KEY => \get_class($dateInterval),
'construct' => array(
Serializer::SCALAR_TYPE => 'string',
Serializer::SCALAR_VALUE => sprintf(
Serializer::SCALAR_VALUE => \sprintf(
'P%sY%sM%sDT%sH%sM%sS',
$dateInterval->y,
$dateInterval->m,
Expand All @@ -90,7 +91,7 @@ public static function serialize(Serializer $serializer, DateInterval $dateInter
Serializer::SCALAR_VALUE => (empty($dateInterval->invert)) ? 0 : 1,
),
'days' => array(
Serializer::SCALAR_TYPE => gettype($dateInterval->days),
Serializer::SCALAR_TYPE => \gettype($dateInterval->days),
Serializer::SCALAR_VALUE => $dateInterval->days,
),
);
Expand Down
1 change: 1 addition & 0 deletions src/Serializer/InternalClasses/DatePeriodSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Serializer\InternalClasses;

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Serializer/InternalClasses/DateTimeZoneSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Serializer\InternalClasses;

use DateTimeZone;
Expand Down Expand Up @@ -45,7 +46,7 @@ public static function unserialize(Serializer $serializer, $className, array $va
$ref = new ReflectionClass($className);

foreach ($value as &$v) {
if (is_array($v)) {
if (\is_array($v)) {
$v = $serializer->unserialize($v);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Strategy/JsonStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Strategy;

/**
Expand All @@ -22,7 +23,7 @@ class JsonStrategy implements StrategyInterface
*/
public function serialize($value)
{
return json_encode($value, JSON_UNESCAPED_UNICODE);
return \json_encode($value, JSON_UNESCAPED_UNICODE);
}

/**
Expand All @@ -32,6 +33,6 @@ public function serialize($value)
*/
public function unserialize($value)
{
return json_decode($value, true);
return \json_decode($value, true);
}
}
1 change: 1 addition & 0 deletions src/Strategy/NullStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Strategy;

/**
Expand Down
1 change: 1 addition & 0 deletions src/Strategy/StrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Strategy;

interface StrategyInterface
Expand Down
23 changes: 12 additions & 11 deletions src/Strategy/XmlStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Strategy;

use NilPortugues\Serializer\Serializer;
Expand Down Expand Up @@ -49,9 +50,9 @@ private static function replaceKeys(array &$replacements, array $input)
{
$return = [];
foreach ($input as $key => $value) {
$key = str_replace(array_keys($replacements), array_values($replacements), $key);
$key = \str_replace(\array_keys($replacements), \array_values($replacements), $key);

if (is_array($value)) {
if (\is_array($value)) {
$value = self::replaceKeys($replacements, $value);
}

Expand All @@ -70,8 +71,8 @@ private static function replaceKeys(array &$replacements, array $input)
private function arrayToXml(array &$data, SimpleXMLElement $xmlData)
{
foreach ($data as $key => $value) {
if (is_array($value)) {
if (is_numeric($key)) {
if (\is_array($value)) {
if (\is_numeric($key)) {
$key = 'np_serializer_element_'.gettype($key).'_'.$key;
}
$subnode = $xmlData->addChild($key);
Expand All @@ -89,10 +90,10 @@ private function arrayToXml(array &$data, SimpleXMLElement $xmlData)
*/
public function unserialize($value)
{
$array = (array) simplexml_load_string($value);
$array = (array) \simplexml_load_string($value);
self::castToArray($array);
self::recoverArrayNumericKeyValues($array);
$replacements = array_flip($this->replacements);
$replacements = \array_flip($this->replacements);
$array = self::replaceKeys($replacements, $array);

return $array;
Expand All @@ -108,7 +109,7 @@ private static function castToArray(array &$array)
$value = (array) $value;
}

if (is_array($value)) {
if (\is_array($value)) {
self::castToArray($value);
}
}
Expand All @@ -121,13 +122,13 @@ private static function recoverArrayNumericKeyValues(array &$array)
{
$newArray = [];
foreach ($array as $key => &$value) {
if (false !== strpos($key, 'np_serializer_element_')) {
if (false !== \strpos($key, 'np_serializer_element_')) {
$key = self::getNumericKeyValue($key);
}

$newArray[$key] = $value;

if (is_array($newArray[$key])) {
if (\is_array($newArray[$key])) {
self::recoverArrayNumericKeyValues($newArray[$key]);
}
}
Expand All @@ -141,8 +142,8 @@ private static function recoverArrayNumericKeyValues(array &$array)
*/
private static function getNumericKeyValue($key)
{
$newKey = str_replace('np_serializer_element_', '', $key);
list($type, $index) = explode('_', $newKey);
$newKey = \str_replace('np_serializer_element_', '', $key);
list($type, $index) = \explode('_', $newKey);

if ('integer' === $type) {
$index = (int) $index;
Expand Down
1 change: 1 addition & 0 deletions src/Strategy/YamlStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NilPortugues\Serializer\Strategy;

use Symfony\Component\Yaml\Yaml;
Expand Down
Loading

0 comments on commit 7d3d211

Please sign in to comment.