Completed
Push — master ( b1072e...d2a3cb )
by Alex
01:47
created

MetadataControllerProvider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 8.97 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 56.52%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 4
dl 7
loc 78
ccs 26
cts 46
cp 0.5652
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C boot() 7 58 12
A register() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Providers;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\Support\Facades\Cache;
7
8
use AlgoWeb\PODataLaravel\Controllers\MetadataControllerContainer;
9
10
class MetadataControllerProvider extends ServiceProvider
11
{
12
    /**
13
     * Bootstrap the application services.  Post-boot.
14
     *
15
     * @return void
16
     */
17 67
    public function boot()
18
    {
19 67
        $isCaching = env('APP_METADATA_CACHING', false);
20
21 67
        if ($isCaching && Cache::has('metadataControllers')) {
22
            $meta = Cache::get('metadataControllers');
23
            $this->app->instance('metadataControllers', $meta);
24
            return;
25
        }
26
27 67
        $meta = $this->app->make('metadataControllers');
28
29 67
        $classes = get_declared_classes();
30 67
        $AutoClass = null;
31 67
        foreach ($classes as $class) {
32 67
            if (\Illuminate\Support\Str::startsWith($class, "Composer\\Autoload\\ComposerStaticInit")) {
33 67
                $AutoClass = $class;
34 67
            }
35 67
        }
36
37 67
        $metamix = [];
38 67
        $ends = array();
39 67
        $Classes = $AutoClass::$classMap;
40 67 View Code Duplication
        foreach ($Classes as $name => $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41 67
            if (\Illuminate\Support\Str::startsWith($name, "App")) {
42
                if (in_array("AlgoWeb\\PODataLaravel\\Controllers\\MetadataControllerTrait", class_uses($name))) {
43
                    $ends[] = $name;
44
                }
45
            }
46 67
        }
47
48
        // now process each class that uses the metadata controller trait and stick results in $metamix
49 67
        $map = null;
0 ignored issues
show
Unused Code introduced by
$map is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50 67
        foreach ($ends as $end) {
51
            $map = $end->getMappings();
52
            // verify uniqueness - must be exactly one mapping for model-verb combo - different verb mappings for
53
            // a model can glom onto different controllers
54
            foreach ($map as $key => $lock) {
55
                if (!array_key_exists($key, $metamix)) {
56
                    // if we haven't yet got a mapping for this model, grab it holus-bolus
57
                    $metamix[$key] = $lock;
58
                    continue;
59
                }
60
                // if we do, make sure we aren't re-adding mappings for any of the CRUD verbs
61
                foreach ($lock as $barrel => $roll) {
62
                    assert(
63
                        !array_key_exists($barrel, $metamix[$key]),
64
                        'Mapping already defined for model '.$key. ' and CRUD verb '.$barrel
65
                    );
66
                    $metamix[$key][$barrel] = $roll;
67
                }
68
69
            }
70 67
        }
71
72 67
        $meta->setMetadata($metamix);
73
74 67
    }
75
76
    /**
77
     * Register the application services.  Boot-time only.
78
     *
79
     * @return void
80
     */
81
    public function register()
82
    {
83 67
        $this->app->singleton('metadataControllers', function ($app) {
1 ignored issue
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84 67
            return new MetadataControllerContainer();
85 67
        });
86 67
    }
87
}
88