Completed
Push — master ( 9498a4...9ba4be )
by CodexShaper
03:54
created

ComposerScripts::postAutoloadDump()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 12
nop 1
dl 0
loc 63
ccs 0
cts 30
cp 0
crap 30
rs 8.4961
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 = basename($root);
45
        $partials = explode('-', $vendor_name);
46
        // $camel_case_partials = array_filter($partials, function($partial){
47
        //     return ucfirst($partial);
48
        // });
49
        $camel_case_partials = [];
50
        foreach ($partials as $partial) {
51
           $camel_case_partials[] = ucfirst(strtolower($partial));
52
        }
53
        // $snake_case_partials = array_filter($partials, function($partial){
54
        //     return strtolower($partial);
55
        // });
56
        $snake_case_partials = [];
57
        foreach ($partials as $partial) {
58
            $snake_case_partials[] = strtolower($partial);
59
        }
60
        $camel_case = implode('_', $camel_case_partials);
61
        $snake_case = implode('_', $snake_case_partials);
62
63
        $files = [
64
            '/wpb.php',
65
            '/bootstrap/app.php',
66
            '/includes/class-wpb-framework-activator.php',
67
            '/includes/class-wpb-framework-deactivator.php',
68
            '/includes/class-wpb-framework-i18n.php',
69
            '/includes/class-wpb-framework-loader.php',
70
            '/includes/class-wpb-framework.php',
71
            '/admin/class-wpb-framework-admin.php',
72
            '/admin/partials/wpb-framework-admin-display.php',
73
            '/admin/css/wpb-framework-admin.css',
74
            '/admin/js/wpb-framework-admin.js',
75
            '/public/class-wpb-framework-public.php',
76
            '/public/partials/wpb-framework-public-display.php',
77
            '/public/css/wpb-framework-public.css',
78
            '/public/js/wpb-framework-public.js',
79
        ];
80
81
        foreach ($files as $file) {
82
            $file = $root.$file;
83
            if(file_exists($file)) {
84
                $contents = file_get_contents($file);
85
                $contents = str_replace('wpb_', $snake_case.'_', $contents);
86
                $contents = str_replace('wpb', $vendor_name, $contents);
87
                $contents = str_replace('WPB', $camel_case, $contents);
88
                file_put_contents(
89
                    $file,
90
                    $contents
91
                );
92
93
                $dir = dirname($file);
94
                $fileName = basename($file);
95
                $newFileName = str_replace('wpb', $vendor_name, $fileName);
96
                rename($file, $dir.'/'.$newFileName.'.php');
97
            }
98
        }
99
    }
100
}
101