Completed
Push — master ( fd3073...5973c1 )
by Greg
09:16
created

ComposerScripts   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testStub() 0 42 2
A languageFiles() 0 16 3
A missingTests() 0 27 4
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2020 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Console;
21
22
use Composer\Script\Event;
23
use Fisharebest\Localization\Translation;
24
use Illuminate\Support\Collection;
25
use League\Flysystem\Adapter\Local;
26
use League\Flysystem\Filesystem;
27
28
use function basename;
29
use function dirname;
30
use function file_put_contents;
31
use function glob;
32
use function str_contains;
33
use function var_export;
34
35
/**
36
 * Command-line utilities.
37
 */
38
class ComposerScripts
39
{
40
    // Location of our translation files.
41
    private const PO_FILE_PATTERN = 'resources/lang/*/*.po';
42
43
    // Path to the root folder.
44
    private const ROOT_DIR = __DIR__ . '/../../';
45
46
    /**
47
     * Rebuild the .POT, .PO and .PHP file.
48
     *
49
     * @param Event $event
50
     */
51
    public static function languageFiles(Event $event): void
52
    {
53
        require $event->getComposer()->getConfig()->get('vendor-dir') . '/autoload.php';
54
55
        $io = $event->getIO();
56
57
        $po_files = glob(self::PO_FILE_PATTERN) ?: [];
58
59
        foreach ($po_files as $po_file) {
60
            $translation  = new Translation($po_file);
61
            $translations = $translation->asArray();
62
            $io->write($po_file . ': ' . count($translations));
63
            $php_file = dirname($po_file) . '/' . basename($po_file, '.po') . '.php';
64
            $php_code = "<?php\n\nreturn " . var_export($translations, true) . ";\n";
65
66
            file_put_contents($php_file, $php_code);
67
        }
68
    }
69
70
    /**
71
     * Ensure every class has a corresponding test script.
72
     *
73
     * @param Event $event
74
     */
75
    public static function missingTests(Event $event): void
76
    {
77
        require $event->getComposer()->getConfig()->get('vendor-dir') . '/autoload.php';
78
79
        $io = $event->getIO();
80
81
        $filesystem = new Filesystem(new Local(self::ROOT_DIR));
82
83
        $scripts = Collection::make($filesystem->listContents('/app/', true))
84
            ->filter(static function (array $file): bool {
85
                return $file['type'] !== 'dir';
86
            })
87
            ->map(static function (array $file): string {
88
                return $file['path'];
89
            })
90
            ->filter(static function (string $script): bool {
91
                return !str_contains($script, 'Interface.php') && !str_contains($script, 'Abstract');
0 ignored issues
show
Deprecated Code introduced by
The function str_contains() has been deprecated: Str::contains() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

91
                return !/** @scrutinizer ignore-deprecated */ str_contains($script, 'Interface.php') && !str_contains($script, 'Abstract');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
92
            })
93
        ;
94
95
        foreach ($scripts as $script) {
96
            $class = strtr($script, ['app/' => '', '.php' => '', '/' => '\\']);
97
            $test  = strtr($script, ['app/' => 'tests/app/', '.php' => 'Test.php']);
98
99
            if (!$filesystem->has($test)) {
100
                $io->write('Creating test script for: ' . $class);
101
                $filesystem->write($test, self::testStub($class));
102
            }
103
        }
104
    }
105
106
    /**
107
     * Create an empty test script.
108
     *
109
     * @param string $class
110
     *
111
     * @return string
112
     */
113
    private static function testStub(string $class): string
114
    {
115
        $year       = date('Y');
116
        $namespace  = strtr(dirname(strtr($class, ['\\' => '/'])), ['/' => '\\']);
117
        $base_class = basename(strtr($class, ['\\' => '/']));
118
119
        if ($namespace === '.') {
120
            $namespace = 'Fisharebest\\Webtrees';
121
        } else {
122
            $namespace = 'Fisharebest\\Webtrees\\' . $namespace;
123
        }
124
125
        return <<<"EOF"
126
<?php
127
128
/**
129
 * webtrees: online genealogy
130
 * Copyright (C) {$year} webtrees development team
131
 * This program is free software: you can redistribute it and/or modify
132
 * it under the terms of the GNU General Public License as published by
133
 * the Free Software Foundation, either version 3 of the License, or
134
 * (at your option) any later version.
135
 * This program is distributed in the hope that it will be useful,
136
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
137
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138
 * GNU General Public License for more details.
139
 * You should have received a copy of the GNU General Public License
140
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
141
 */
142
143
declare(strict_types=1);
144
145
namespace {$namespace};
146
147
use Fisharebest\\Webtrees\\TestCase;
148
149
/**
150
 * Test harness for the class {$base_class}
151
 *
152
 * @covers {$namespace}\\{$base_class}
153
 */
154
class {$base_class}Test extends TestCase
155
{
156
}
157
158
EOF;
159
    }
160
}
161