setUrlWithReflection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4286
cc 2
eloc 7
nc 2
nop 3
1
<?php
2
3
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 8/15/15
6
 * Time: 5:45 PM.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace NilPortugues\Laravel5\HalJsonSerializer;
13
14
use Illuminate\Support\Facades\Cache;
15
use Illuminate\Support\ServiceProvider;
16
use NilPortugues\Api\HalJson\HalJsonTransformer;
17
use NilPortugues\Api\Mapping\Mapping;
18
use NilPortugues\Laravel5\HalJsonSerializer\Mapper\Mapper;
19
use ReflectionClass;
20
21
class Laravel5HalJsonSerializerServiceProvider extends ServiceProvider
22
{
23
    const PATH = '/../../../config/haljson.php';
24
25
    /**
26
     * Indicates if loading of the provider is deferred.
27
     *
28
     * @var bool
29
     */
30
    protected $defer = false;
31
32
    /**
33
     * Bootstrap the application events.
34
     */
35
    public function boot()
36
    {
37
        $this->publishes([__DIR__.self::PATH => config('haljson.php')]);
38
    }
39
40
    /**
41
     * Register the service provider.
42
     */
43
    public function register()
44
    {
45
        $this->mergeConfigFrom(__DIR__.self::PATH, 'haljson');
46
        $this->app->singleton(
47
            \NilPortugues\Laravel5\HalJsonSerializer\HalJsonSerializer::class,
48
            function ($app) {
49
                $mapping = $app['config']->get('haljson');
50
                $key = \md5(\json_encode($mapping));
51
52
                return Cache::rememberForever(
53
                    $key,
54
                    function () use ($mapping) {
55
                        return new HalJsonSerializer(new HalJsonTransformer(self::parseRoutes(new Mapper($mapping))));
56
                    }
57
                );
58
            }
59
        );
60
    }
61
62
    /**
63
     * @param Mapper $mapper
64
     *
65
     * @return Mapper
66
     */
67
    private static function parseRoutes(Mapper $mapper)
68
    {
69
        foreach ($mapper->getClassMap() as &$mapping) {
0 ignored issues
show
Bug introduced by
The expression $mapper->getClassMap() cannot be used as a reference.

Let?s assume that you have the following foreach statement:

foreach ($array as &$itemValue) { }

$itemValue is assigned by reference. This is possible because the expression (in the example $array) can be used as a reference target.

However, if we were to replace $array with something different like the result of a function call as in

foreach (getArray() as &$itemValue) { }

then assigning by reference is not possible anymore as there is no target that could be modified.

Available Fixes

1. Do not assign by reference
foreach (getArray() as $itemValue) { }
2. Assign to a local variable first
$array = getArray();
foreach ($array as &$itemValue) {}
3. Return a reference
function &getArray() { $array = array(); return $array; }

foreach (getArray() as &$itemValue) { }
Loading history...
70
            $mappingClass = new \ReflectionClass($mapping);
71
72
            self::setUrlWithReflection($mapping, $mappingClass, 'resourceUrlPattern');
73
            self::setUrlWithReflection($mapping, $mappingClass, 'selfUrl');
74
            $mappingProperty = $mappingClass->getProperty('otherUrls');
75
            $mappingProperty->setAccessible(true);
76
77
            $otherUrls = (array) $mappingProperty->getValue($mapping);
78
            if (!empty($otherUrls)) {
79
                foreach ($otherUrls as &$url) {
80
                    $url = \urldecode(route($url));
81
                }
82
            }
83
            $mappingProperty->setValue($mapping, $otherUrls);
84
        }
85
86
        return $mapper;
87
    }
88
89
    /**
90
     * @param Mapping         $mapping
91
     * @param ReflectionClass $mappingClass
92
     * @param string          $property
93
     */
94
    private static function setUrlWithReflection(Mapping $mapping, ReflectionClass $mappingClass, $property)
95
    {
96
        $mappingProperty = $mappingClass->getProperty($property);
97
        $mappingProperty->setAccessible(true);
98
        $value = $mappingProperty->getValue($mapping);
99
100
        if (!empty($value)) {
101
            $value = \urldecode(route($value));
102
            $mappingProperty->setValue($mapping, $value);
103
        }
104
    }
105
106
    /**
107
     * Get the services provided by the provider.
108
     *
109
     * @return array
110
     */
111
    public function provides()
112
    {
113
        return ['haljson'];
114
    }
115
}
116