From 89bc2a5f5a25af05d75dcf285ffc53765675e00f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nil=20Portugu=C3=A9s=20Calder=C3=B3?= Date: Mon, 2 Nov 2015 23:24:24 +0100 Subject: [PATCH] Added backslash to PHP internal functions --- composer.json | 5 +++ .../Facades/JsonApiSerializer.php | 1 + .../JsonApiResponseTrait.php | 5 +-- .../JsonApiSerializer/JsonApiSerializer.php | 35 ++++++++----------- ...ravel5JsonApiSerializerServiceProvider.php | 23 ++++++------ .../JsonApiSerializer/Mapper/Mapper.php | 4 +-- .../Mapper/MappingFactory.php | 25 ++++++------- 7 files changed, 47 insertions(+), 51 deletions(-) diff --git a/composer.json b/composer.json index 8ec39bd..9b33f24 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,11 @@ "nilportugues/json-api": "~1.0", "symfony/psr-http-message-bridge": "~0.2" }, + "require-dev": { + "laravel/laravel": "5.*", + "fabpot/php-cs-fixer": "~1.9", + "nilportugues/php_backslasher": "~0.2" + }, "autoload": { "psr-4": { "NilPortugues\\Laravel5\\JsonApiSerializer\\" : "src/NilPortugues/Laravel5/JsonApiSerializer/" diff --git a/src/NilPortugues/Laravel5/JsonApiSerializer/Facades/JsonApiSerializer.php b/src/NilPortugues/Laravel5/JsonApiSerializer/Facades/JsonApiSerializer.php index e8ef459..cebbfdc 100644 --- a/src/NilPortugues/Laravel5/JsonApiSerializer/Facades/JsonApiSerializer.php +++ b/src/NilPortugues/Laravel5/JsonApiSerializer/Facades/JsonApiSerializer.php @@ -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\Laravel5\JsonApiSerializer\Facades; use Illuminate\Support\Facades\Facade; diff --git a/src/NilPortugues/Laravel5/JsonApiSerializer/JsonApiResponseTrait.php b/src/NilPortugues/Laravel5/JsonApiSerializer/JsonApiResponseTrait.php index d99e85f..39be220 100644 --- a/src/NilPortugues/Laravel5/JsonApiSerializer/JsonApiResponseTrait.php +++ b/src/NilPortugues/Laravel5/JsonApiSerializer/JsonApiResponseTrait.php @@ -2,7 +2,7 @@ /** * Author: Nil Portugués Calderó * Date: 8/18/15 - * Time: 11:19 PM + * Time: 11:19 PM. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -15,7 +15,8 @@ trait JsonApiResponseTrait { /** - * @param \Psr\Http\Message\ResponseInterface $response + * @param \Psr\Http\Message\ResponseInterface $response + * * @return \Psr\Http\Message\ResponseInterface */ protected function addHeaders(\Psr\Http\Message\ResponseInterface $response) diff --git a/src/NilPortugues/Laravel5/JsonApiSerializer/JsonApiSerializer.php b/src/NilPortugues/Laravel5/JsonApiSerializer/JsonApiSerializer.php index 5b45948..28405d9 100644 --- a/src/NilPortugues/Laravel5/JsonApiSerializer/JsonApiSerializer.php +++ b/src/NilPortugues/Laravel5/JsonApiSerializer/JsonApiSerializer.php @@ -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\Laravel5\JsonApiSerializer; use ErrorException; @@ -48,20 +49,19 @@ protected function serializeObject($value) return [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items]; } - if (is_subclass_of($value, Model::class, true)) { - - $stdClass = (object) $value->getAttributes(); - $data = $this->serializeData($stdClass); - $data[self::CLASS_IDENTIFIER_KEY] = get_class($value); + if (\is_subclass_of($value, Model::class, true)) { + $stdClass = (object) $value->getAttributes(); + $data = $this->serializeData($stdClass); + $data[self::CLASS_IDENTIFIER_KEY] = \get_class($value); $methods = $this->getRelationshipMethodsAsPropertyName( $value, - get_class($value), + \get_class($value), new ReflectionClass($value) ); if (!empty($methods)) { - $data = array_merge($data, $methods); + $data = \array_merge($data, $methods); } return $data; @@ -79,12 +79,10 @@ protected function serializeObject($value) */ protected function getRelationshipMethodsAsPropertyName($value, $className, ReflectionClass $reflection) { - $methods = []; foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { - if (ltrim($method->class, "\\") === ltrim($className, "\\")) { - - $name = $method->name; + if (\ltrim($method->class, '\\') === \ltrim($className, '\\')) { + $name = $method->name; $reflectionMethod = $reflection->getMethod($name); // Eloquent relations do not include parameters, so we'll be filtering based on this criteria. @@ -92,19 +90,17 @@ protected function getRelationshipMethodsAsPropertyName($value, $className, Refl try { $returned = $reflectionMethod->invoke($value); //All operations (eg: boolean operations) are now filtered out. - if (is_object($returned)) { + if (\is_object($returned)) { // Only keep those methods as properties if these are returning Eloquent relations. // But do not run the operation as it is an expensive operation. - if (false !== strpos(get_class($returned), 'Illuminate\Database\Eloquent\Relations')) { - + if (false !== \strpos(\get_class($returned), 'Illuminate\Database\Eloquent\Relations')) { $items = []; foreach ($returned->getResults() as $model) { - - if (is_object($model)) { - $stdClass = (object) $model->getAttributes(); - $data = $this->serializeData($stdClass); - $data[self::CLASS_IDENTIFIER_KEY] = get_class($model); + if (\is_object($model)) { + $stdClass = (object) $model->getAttributes(); + $data = $this->serializeData($stdClass); + $data[self::CLASS_IDENTIFIER_KEY] = \get_class($model); $items[] = $data; } @@ -112,7 +108,6 @@ protected function getRelationshipMethodsAsPropertyName($value, $className, Refl if (!empty($items)) { $methods[$name] = [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items]; } - } } } catch (ErrorException $e) { diff --git a/src/NilPortugues/Laravel5/JsonApiSerializer/Laravel5JsonApiSerializerServiceProvider.php b/src/NilPortugues/Laravel5/JsonApiSerializer/Laravel5JsonApiSerializerServiceProvider.php index 6c3e0e1..ffb004c 100644 --- a/src/NilPortugues/Laravel5/JsonApiSerializer/Laravel5JsonApiSerializerServiceProvider.php +++ b/src/NilPortugues/Laravel5/JsonApiSerializer/Laravel5JsonApiSerializerServiceProvider.php @@ -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\Laravel5\JsonApiSerializer; use Illuminate\Support\Facades\Cache; @@ -33,7 +34,7 @@ class Laravel5JsonApiSerializerServiceProvider extends ServiceProvider */ public function boot() { - $this->publishes([__DIR__ . self::PATH => config('jsonapi.php')]); + $this->publishes([__DIR__.self::PATH => config('jsonapi.php')]); } /** @@ -41,13 +42,13 @@ public function boot() */ public function register() { - $this->mergeConfigFrom(__DIR__ . self::PATH, 'jsonapi'); + $this->mergeConfigFrom(__DIR__.self::PATH, 'jsonapi'); $this->app->singleton( \NilPortugues\Laravel5\JsonApiSerializer\JsonApiSerializer::class, function ($app) { $mapping = $app['config']->get('jsonapi'); - $key = md5(json_encode($mapping)); + $key = \md5(\json_encode($mapping)); return Cache::rememberForever( $key, @@ -67,7 +68,6 @@ function () use ($mapping) { private static function parseRoutes(Mapper $mapper) { foreach ($mapper->getClassMap() as &$mapping) { - $mappingClass = new \ReflectionClass($mapping); self::setUrlWithReflection($mapping, $mappingClass, 'resourceUrlPattern'); @@ -76,9 +76,9 @@ private static function parseRoutes(Mapper $mapper) $mappingProperty->setAccessible(true); $otherUrls = (array) $mappingProperty->getValue($mapping); - if(!empty($otherUrls)) { + if (!empty($otherUrls)) { foreach ($otherUrls as &$url) { - $url = urldecode(route($url)); + $url = \urldecode(route($url)); } } $mappingProperty->setValue($mapping, $otherUrls); @@ -88,11 +88,11 @@ private static function parseRoutes(Mapper $mapper) $mappingProperty->setAccessible(true); $relationshipSelfUrl = (array) $mappingProperty->getValue($mapping); - if(!empty($relationshipSelfUrl)) { + if (!empty($relationshipSelfUrl)) { foreach ($relationshipSelfUrl as &$urlMember) { - if(!empty($urlMember)) { + if (!empty($urlMember)) { foreach ($urlMember as &$url) { - $url = urldecode(route($url)); + $url = \urldecode(route($url)); } } } @@ -103,7 +103,6 @@ private static function parseRoutes(Mapper $mapper) return $mapper; } - /** * @param Mapping $mapping * @param ReflectionClass $mappingClass @@ -115,8 +114,8 @@ private static function setUrlWithReflection(Mapping $mapping, ReflectionClass $ $mappingProperty->setAccessible(true); $value = $mappingProperty->getValue($mapping); - if(!empty($value)) { - $value = urldecode(route($value)); + if (!empty($value)) { + $value = \urldecode(route($value)); $mappingProperty->setValue($mapping, $value); } } diff --git a/src/NilPortugues/Laravel5/JsonApiSerializer/Mapper/Mapper.php b/src/NilPortugues/Laravel5/JsonApiSerializer/Mapper/Mapper.php index 814b1cc..41aadee 100644 --- a/src/NilPortugues/Laravel5/JsonApiSerializer/Mapper/Mapper.php +++ b/src/NilPortugues/Laravel5/JsonApiSerializer/Mapper/Mapper.php @@ -2,7 +2,7 @@ /** * Author: Nil Portugués Calderó * Date: 10/16/15 - * Time: 8:59 PM + * Time: 8:59 PM. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -19,7 +19,7 @@ class Mapper extends \NilPortugues\Api\Mapping\Mapper */ protected function buildMapping($mappedClass) { - return (is_string($mappedClass) && class_exists($mappedClass, true)) ? + return (\is_string($mappedClass) && \class_exists($mappedClass, true)) ? MappingFactory::fromClass($mappedClass) : MappingFactory::fromArray($mappedClass); } diff --git a/src/NilPortugues/Laravel5/JsonApiSerializer/Mapper/MappingFactory.php b/src/NilPortugues/Laravel5/JsonApiSerializer/Mapper/MappingFactory.php index 2e0b7f6..f393499 100644 --- a/src/NilPortugues/Laravel5/JsonApiSerializer/Mapper/MappingFactory.php +++ b/src/NilPortugues/Laravel5/JsonApiSerializer/Mapper/MappingFactory.php @@ -2,7 +2,7 @@ /** * Author: Nil Portugués Calderó * Date: 10/16/15 - * Time: 8:59 PM + * Time: 8:59 PM. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -17,8 +17,7 @@ use ReflectionMethod; /** - * Class MappingFactory - * @package NilPortugues\Laravel5\JsonApiSerializer\Mapper + * Class MappingFactory. */ class MappingFactory extends \NilPortugues\Api\Mapping\MappingFactory { @@ -34,12 +33,12 @@ class MappingFactory extends \NilPortugues\Api\Mapping\MappingFactory */ protected static function getClassProperties($className) { - if (class_exists($className, true)) { + if (\class_exists($className, true)) { $reflection = new ReflectionClass($className); - $value = $reflection->newInstanceWithoutConstructor(); + $value = $reflection->newInstanceWithoutConstructor(); - if (is_subclass_of($value, Model::class, true)) { - $attributes = array_merge( + if (\is_subclass_of($value, Model::class, true)) { + $attributes = \array_merge( Schema::getColumnListing($value->getTable()), self::getRelationshipMethodsAsPropertyName($value, $className, $reflection) ); @@ -48,7 +47,6 @@ protected static function getClassProperties($className) return self::$eloquentClasses[$className]; } - } return parent::getClassProperties($className); @@ -65,10 +63,8 @@ protected static function getRelationshipMethodsAsPropertyName($value, $classNam { $methods = []; foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { - - if (ltrim($method->class, "\\") === ltrim($className, "\\")) { - - $name = $method->name; + if (\ltrim($method->class, '\\') === \ltrim($className, '\\')) { + $name = $method->name; $reflectionMethod = $reflection->getMethod($name); // Eloquent relations do not include parameters, so we'll be filtering based on this criteria. @@ -76,14 +72,13 @@ protected static function getRelationshipMethodsAsPropertyName($value, $classNam try { $returned = $reflectionMethod->invoke($value); //All operations (eg: boolean operations) are now filtered out. - if (is_object($returned)) { + if (\is_object($returned)) { // Only keep those methods as properties if these are returning Eloquent relations. // But do not run the operation as it is an expensive operation. - if (false !== strpos(get_class($returned), 'Illuminate\Database\Eloquent\Relations')) { + if (false !== \strpos(\get_class($returned), 'Illuminate\Database\Eloquent\Relations')) { $methods[] = $method->name; } - } } catch (ErrorException $e) { }