Completed
Push — master ( b81a63...5c5ce9 )
by CodexShaper
03:29
created

ComposerScripts::updateComposer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 13
loc 13
ccs 0
cts 8
cp 0
crap 6
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace CodexShaper\WP;
4
5
use Composer\Script\Event;
6
7
class ComposerScripts
8
{
9
    /**
10
     * Handle the post-install Composer event.
11
     *
12
     * @param  \Composer\Script\Event  $event
13
     * @return void
14
     */
15
    public static function postInstall(Event $event)
16
    {
17
        require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php';
18
    }
19
20
    /**
21
     * Handle the post-update Composer event.
22
     *
23
     * @param  \Composer\Script\Event  $event
24
     * @return void
25
     */
26
    public static function postUpdate(Event $event)
27
    {
28
        require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php';
29
    }
30
31
    /**
32
     * Handle the post-autoload-dump Composer event.
33
     *
34
     * @param  \Composer\Script\Event  $event
35
     * @return void
36
     */
37
    public static function postAutoloadDump(Event $event)
38
    {
39
        require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php';
40
41
        $dir = $event->getComposer()->getConfig()->get('vendor-dir').'/../';
0 ignored issues
show
Unused Code introduced by
$dir 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...
42
        $root = dirname($event->getComposer()->getConfig()->get('vendor-dir'));
43
44
        $vendor_name = strtolower(basename($root));
45
        $partials = explode('-', $vendor_name);
46
        $camel_case_partials = [];
47
        foreach ($partials as $partial) {
48
           $camel_case_partials[] = ucfirst(strtolower($partial));
49
        }
50
        $camel_case = implode('_', $camel_case_partials);
51
        $snake_case = implode('_', $partials);
52
53
        $files = [
54
            '/wpb.php',
55
            '/bootstrap/app.php',
56
            '/includes/class-wpb-activator.php',
57
            '/includes/class-wpb-deactivator.php',
58
            '/includes/class-wpb-i18n.php',
59
            '/includes/class-wpb-loader.php',
60
            '/includes/class-wpb.php',
61
            '/admin/class-wpb-admin.php',
62
            '/admin/class-wpb-admin-menu.php',
63
            '/admin/class-wpb-admin-submenu.php',
64
            '/admin/partials/wpb-admin-display.php',
65
            '/admin/css/wpb-admin.css',
66
            '/admin/js/wpb-admin.js',
67
            '/public/class-wpb-public.php',
68
            '/public/partials/wpb-public-display.php',
69
            '/public/css/wpb-public.css',
70
            '/public/js/wpb-public.js',
71
            '/resources/js/admin/main.js',
72
            '/resources/js/frontend/main.js',
73
            '/resources/js/spa/main.js',
74
            '/src/Application.php',
75
            '/src/helpers.php',
76
            '/src/Support/Facades/Config.php',
77
            '/src/Support/Facades/Route.php',
78
            '/src/Http/Kernel.php',
79
            '/src/Http/Events/RequestHandler.php',
80
            '/src/Exceptions/Handler.php',
81
            '/app/User.php',
82
            '/app/Post.php',
83
            '/app/Http/Controllers/ProductController.php',
84
            '/app/Http/Middleware/AuthMiddleware.php',
85
            '/app/Http/Middleware/VerifyCsrfToken.php',
86
            '/app/Http/Kernel.php',
87
            '/app/Exceptions/Handler.php',
88
        ];
89
90
        foreach ($files as $file) {
91
            $file = $root.$file;
92
            if(file_exists($file)) {
93
                $contents = file_get_contents($file);
94
                $contents = str_replace('wpb_', $snake_case.'_', $contents);
95
                $contents = str_replace('wpb', $vendor_name, $contents);
96
                $contents = str_replace('WPB_APP_ROOT', strtoupper($camel_case).'_APP_ROOT', $contents);
97
                $contents = str_replace('WPB_FILE', strtoupper($camel_case).'_FILE', $contents);
98
                $contents = str_replace('WPB_PATH', strtoupper($camel_case).'_PATH', $contents);
99
                $contents = str_replace('WPB_INCLUDES', strtoupper($camel_case).'_INCLUDES', $contents);
100
                $contents = str_replace('WPB_URL', strtoupper($camel_case).'_URL', $contents);
101
                $contents = str_replace('WPB_ASSETS', strtoupper($camel_case).'_ASSETS', $contents);
102
                $contents = str_replace('WPB_VERSION', strtoupper($camel_case).'_VERSION', $contents);
103
                $contents = str_replace('WPB', $camel_case, $contents);
104
                file_put_contents(
105
                    $file,
106
                    $contents
107
                );
108
109
                $dir = dirname($file);
110
                $fileName = basename($file);
111
                $newFileName = str_replace('wpb', $vendor_name, $fileName);
112
                
113
                if($fileName != $newFileName) {
114
                    rename($file, $dir.'/'.$newFileName);
115
                }
116
                
117
            }
118
        }
119
120
        // static::configApp($root, $camel_case);
121
        static::updateComposer($root, $camel_case);
122
    }
123
124 View Code Duplication
    protected static function configApp($root, $camel_case)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
125
    {
126
        $file = $root.'/bootstrap/app.php';
127
        if(file_exists($file)) {
128
            $contents = file_get_contents($file);
129
            $contents = str_replace('WPB_APP_ROOT', strtoupper($camel_case).'_APP_ROOT', $contents);
130
            file_put_contents(
131
                $file,
132
                $contents
133
            );
134
            
135
        }
136
    }
137
138 View Code Duplication
    protected static function updateComposer($root, $camel_case)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
139
    {
140
        $file = $root.'/composer.json';
141
        if(file_exists($file)) {
142
            $contents = file_get_contents($file);
143
            $contents = str_replace('WPB', strtoupper($camel_case), $contents);
144
            file_put_contents(
145
                $file,
146
                $contents
147
            );
148
            
149
        }
150
    }
151
}
152