Skip to content

Commit 9150bcb

Browse files
authored
Add WordPress adapter (#378)
* Add WordPress adapter * Fix adapter namespace and add WordPress to ConfigResolver * Add tests for config resolver
1 parent 3f6be59 commit 9150bcb

File tree

5 files changed

+230
-0
lines changed

5 files changed

+230
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NunoMaduro\PhpInsights\Application\Adapters\WordPress;
6+
7+
use NunoMaduro\PhpInsights\Application\Composer;
8+
use NunoMaduro\PhpInsights\Application\ConfigResolver;
9+
use NunoMaduro\PhpInsights\Application\DefaultPreset;
10+
use NunoMaduro\PhpInsights\Domain\Contracts\Preset as PresetContract;
11+
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
12+
13+
/**
14+
* @internal
15+
*/
16+
final class Preset implements PresetContract
17+
{
18+
public static function getName(): string
19+
{
20+
return 'wordpress';
21+
}
22+
23+
public static function get(Composer $composer): array
24+
{
25+
$config = [
26+
'exclude' => [
27+
'web/wp',
28+
'web/.htaccess',
29+
'web/app/mu-plugins/',
30+
'web/app/upgrade',
31+
'web/app/uploads/',
32+
'web/app/plugins/',
33+
],
34+
'config' => [
35+
ForbiddenFunctionsSniff::class => [
36+
'forbiddenFunctions' => [
37+
'eval' => null,
38+
'error_log' => null,
39+
'print_r' => null,
40+
],
41+
],
42+
],
43+
];
44+
45+
return ConfigResolver::mergeConfig(DefaultPreset::get($composer), $config);
46+
}
47+
48+
public static function shouldBeApplied(Composer $composer): bool
49+
{
50+
$requirements = $composer->getRequirements();
51+
52+
foreach (array_keys($requirements) as $requirement) {
53+
$requirement = (string) $requirement;
54+
55+
if (strpos($requirement, 'johnpbloch/wordpress') !== false
56+
|| strpos($requirement, 'roots/wordpress') !== false) {
57+
return true;
58+
}
59+
}
60+
61+
return false;
62+
}
63+
}

src/Application/ConfigResolver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use NunoMaduro\PhpInsights\Application\Adapters\Laravel\Preset as LaravelPreset;
99
use NunoMaduro\PhpInsights\Application\Adapters\Magento2\Preset as Magento2Preset;
1010
use NunoMaduro\PhpInsights\Application\Adapters\Symfony\Preset as SymfonyPreset;
11+
use NunoMaduro\PhpInsights\Application\Adapters\WordPress\Preset as WordPressPreset;
1112
use NunoMaduro\PhpInsights\Application\Adapters\Yii\Preset as YiiPreset;
1213
use NunoMaduro\PhpInsights\Application\Console\Formatters\PathShortener;
1314
use NunoMaduro\PhpInsights\Domain\Configuration;
@@ -34,6 +35,7 @@ final class ConfigResolver
3435
SymfonyPreset::class,
3536
YiiPreset::class,
3637
Magento2Preset::class,
38+
WordPressPreset::class,
3739
DefaultPreset::class,
3840
];
3941

stubs/wordpress.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
7+
/*
8+
|--------------------------------------------------------------------------
9+
| Default Preset
10+
|--------------------------------------------------------------------------
11+
|
12+
| This option controls the default preset that will be used by PHP Insights
13+
| to make your code reliable, simple, and clean. However, you can always
14+
| adjust the `Metrics` and `Insights` below in this configuration file.
15+
|
16+
| Supported: "default", "laravel", "symfony", "magento2", "drupal", "wordpress"
17+
|
18+
*/
19+
20+
'preset' => 'wordpress',
21+
/*
22+
|--------------------------------------------------------------------------
23+
| IDE
24+
|--------------------------------------------------------------------------
25+
|
26+
| This options allow to add hyperlinks in your terminal to quickly open
27+
| files in your favorite IDE while browsing your PhpInsights report.
28+
|
29+
| Supported: "textmate", "macvim", "emacs", "sublime", "phpstorm",
30+
| "atom", "vscode".
31+
|
32+
| If you have another IDE that is not in this list but which provide an
33+
| url-handler, you could fill this config with a pattern like this:
34+
|
35+
| myide://open?url=file://%f&line=%l
36+
|
37+
*/
38+
39+
'ide' => null,
40+
/*
41+
|--------------------------------------------------------------------------
42+
| Configuration
43+
|--------------------------------------------------------------------------
44+
|
45+
| Here you may adjust all the various `Insights` that will be used by PHP
46+
| Insights. You can either add, remove or configure `Insights`. Keep in
47+
| mind, that all added `Insights` must belong to a specific `Metric`.
48+
|
49+
*/
50+
51+
'exclude' => [
52+
// 'path/to/directory-or-file'
53+
],
54+
55+
'add' => [
56+
// ExampleMetric::class => [
57+
// ExampleInsight::class,
58+
// ]
59+
],
60+
61+
'remove' => [
62+
// ExampleInsight::class,
63+
],
64+
65+
'config' => [
66+
// ExampleInsight::class => [
67+
// 'key' => 'value',
68+
// ],
69+
],
70+
71+
/*
72+
|--------------------------------------------------------------------------
73+
| Requirements
74+
|--------------------------------------------------------------------------
75+
|
76+
| Here you may define a level you want to reach per `Insights` category.
77+
| When a score is lower than the minimum level defined, then an error
78+
| code will be returned. This is optional and individually defined.
79+
|
80+
*/
81+
82+
'requirements' => [
83+
// 'min-quality' => 0,
84+
// 'min-complexity' => 0,
85+
// 'min-architecture' => 0,
86+
// 'min-style' => 0,
87+
// 'disable-security-check' => false,
88+
],
89+
90+
];

tests/Application/ConfigResolverTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ public function testGuessDrupal(): void
9191
self::assertSame('drupal', $preset);
9292
}
9393

94+
public function testGuessWordPress(): void
95+
{
96+
$preset = ConfigResolver::guess(
97+
Composer::fromPath($this->baseFixturePath . 'ComposerWordPress' . DIRECTORY_SEPARATOR . 'composer.json')
98+
);
99+
self::assertSame('wordpress', $preset);
100+
}
101+
94102
public function testResolvedConfigIsCorrectlyMerged(): void
95103
{
96104
$config = [
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"name": "roots/bedrock",
3+
"type": "project",
4+
"license": "MIT",
5+
"description": "WordPress boilerplate with modern development tools, easier configuration, and an improved folder structure",
6+
"homepage": "https://roots.io/bedrock/",
7+
"authors": [
8+
{
9+
"name": "Scott Walkinshaw",
10+
"email": "scott.walkinshaw@gmail.com",
11+
"homepage": "https://github.com/swalkinshaw"
12+
},
13+
{
14+
"name": "Ben Word",
15+
"email": "ben@benword.com",
16+
"homepage": "https://github.com/retlehs"
17+
}
18+
],
19+
"keywords": [
20+
"bedrock", "composer", "roots", "wordpress", "wp", "wp-config"
21+
],
22+
"support": {
23+
"issues": "https://github.com/roots/bedrock/issues",
24+
"forum": "https://discourse.roots.io/category/bedrock"
25+
},
26+
"repositories": [
27+
{
28+
"type": "composer",
29+
"url": "https://wpackagist.org"
30+
}
31+
],
32+
"require": {
33+
"php": ">=7.1",
34+
"composer/installers": "^1.8",
35+
"vlucas/phpdotenv": "^4.1.0",
36+
"oscarotero/env": "^1.2.0",
37+
"roots/wordpress": "5.3.2",
38+
"roots/wp-config": "1.0.0",
39+
"roots/wp-password-bcrypt": "1.0.0"
40+
},
41+
"require-dev": {
42+
"squizlabs/php_codesniffer": "^3.5.4",
43+
"roave/security-advisories": "dev-master"
44+
},
45+
"config": {
46+
"optimize-autoloader": true,
47+
"preferred-install": "dist"
48+
},
49+
"minimum-stability": "dev",
50+
"prefer-stable": true,
51+
"extra": {
52+
"installer-paths": {
53+
"web/app/mu-plugins/{$name}/": ["type:wordpress-muplugin"],
54+
"web/app/plugins/{$name}/": ["type:wordpress-plugin"],
55+
"web/app/themes/{$name}/": ["type:wordpress-theme"]
56+
},
57+
"wordpress-install-dir": "web/wp"
58+
},
59+
"scripts": {
60+
"post-root-package-install": [
61+
"php -r \"copy('.env.example', '.env');\""
62+
],
63+
"test": [
64+
"phpcs"
65+
]
66+
}
67+
}

0 commit comments

Comments
 (0)