From 66c234c07b960952d20e8177a4e8882ad7d332dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20=C5=BDoljom?= Date: Thu, 12 Mar 2020 17:06:58 +0100 Subject: [PATCH 1/3] Add WordPress adapter --- src/Application/Adapters/WordPress/Preset.php | 63 +++++++++++++ stubs/wordpress.php | 90 +++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 src/Application/Adapters/WordPress/Preset.php create mode 100644 stubs/wordpress.php diff --git a/src/Application/Adapters/WordPress/Preset.php b/src/Application/Adapters/WordPress/Preset.php new file mode 100644 index 00000000..15f1eb53 --- /dev/null +++ b/src/Application/Adapters/WordPress/Preset.php @@ -0,0 +1,63 @@ + [ + 'web/wp', + 'web/.htaccess', + 'web/app/mu-plugins/', + 'web/app/upgrade', + 'web/app/uploads/', + 'web/app/plugins/', + ], + 'config' => [ + ForbiddenFunctionsSniff::class => [ + 'forbiddenFunctions' => [ + 'eval' => null, + 'error_log' => null, + 'print_r' => null, + ], + ], + ], + ]; + + return ConfigResolver::mergeConfig(DefaultPreset::get($composer), $config); + } + + public static function shouldBeApplied(Composer $composer): bool + { + $requirements = $composer->getRequirements(); + + foreach (array_keys($requirements) as $requirement) { + $requirement = (string) $requirement; + + if (strpos($requirement, 'johnpbloch/wordpress') !== false + || strpos($requirement, 'roots/wordpress') !== false) { + return true; + } + } + + return false; + } +} diff --git a/stubs/wordpress.php b/stubs/wordpress.php new file mode 100644 index 00000000..1b425ca6 --- /dev/null +++ b/stubs/wordpress.php @@ -0,0 +1,90 @@ + 'wordpress', + /* + |-------------------------------------------------------------------------- + | IDE + |-------------------------------------------------------------------------- + | + | This options allow to add hyperlinks in your terminal to quickly open + | files in your favorite IDE while browsing your PhpInsights report. + | + | Supported: "textmate", "macvim", "emacs", "sublime", "phpstorm", + | "atom", "vscode". + | + | If you have another IDE that is not in this list but which provide an + | url-handler, you could fill this config with a pattern like this: + | + | myide://open?url=file://%f&line=%l + | + */ + + 'ide' => null, + /* + |-------------------------------------------------------------------------- + | Configuration + |-------------------------------------------------------------------------- + | + | Here you may adjust all the various `Insights` that will be used by PHP + | Insights. You can either add, remove or configure `Insights`. Keep in + | mind, that all added `Insights` must belong to a specific `Metric`. + | + */ + + 'exclude' => [ + // 'path/to/directory-or-file' + ], + + 'add' => [ + // ExampleMetric::class => [ + // ExampleInsight::class, + // ] + ], + + 'remove' => [ + // ExampleInsight::class, + ], + + 'config' => [ + // ExampleInsight::class => [ + // 'key' => 'value', + // ], + ], + + /* + |-------------------------------------------------------------------------- + | Requirements + |-------------------------------------------------------------------------- + | + | Here you may define a level you want to reach per `Insights` category. + | When a score is lower than the minimum level defined, then an error + | code will be returned. This is optional and individually defined. + | + */ + + 'requirements' => [ +// 'min-quality' => 0, +// 'min-complexity' => 0, +// 'min-architecture' => 0, +// 'min-style' => 0, +// 'disable-security-check' => false, + ], + +]; From 219bec8ab9df1a44b64b94c5f2fb48dea4cfb869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20=C5=BDoljom?= Date: Thu, 12 Mar 2020 18:04:34 +0100 Subject: [PATCH 2/3] Fix adapter namespace and add WordPress to ConfigResolver --- src/Application/Adapters/WordPress/Preset.php | 2 +- src/Application/ConfigResolver.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Application/Adapters/WordPress/Preset.php b/src/Application/Adapters/WordPress/Preset.php index 15f1eb53..9441825c 100644 --- a/src/Application/Adapters/WordPress/Preset.php +++ b/src/Application/Adapters/WordPress/Preset.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace NunoMaduro\PhpInsights\Application\Adapters\Symfony; +namespace NunoMaduro\PhpInsights\Application\Adapters\WordPress; use NunoMaduro\PhpInsights\Application\Composer; use NunoMaduro\PhpInsights\Application\ConfigResolver; diff --git a/src/Application/ConfigResolver.php b/src/Application/ConfigResolver.php index d8c47895..1bde9cbe 100644 --- a/src/Application/ConfigResolver.php +++ b/src/Application/ConfigResolver.php @@ -8,6 +8,7 @@ use NunoMaduro\PhpInsights\Application\Adapters\Laravel\Preset as LaravelPreset; use NunoMaduro\PhpInsights\Application\Adapters\Magento2\Preset as Magento2Preset; use NunoMaduro\PhpInsights\Application\Adapters\Symfony\Preset as SymfonyPreset; +use NunoMaduro\PhpInsights\Application\Adapters\WordPress\Preset as WordPressPreset; use NunoMaduro\PhpInsights\Application\Adapters\Yii\Preset as YiiPreset; use NunoMaduro\PhpInsights\Domain\Configuration; use NunoMaduro\PhpInsights\Domain\Contracts\Preset; @@ -32,6 +33,7 @@ final class ConfigResolver SymfonyPreset::class, YiiPreset::class, Magento2Preset::class, + WordPressPreset::class, DefaultPreset::class, ]; From a341b7ef1eded697c3e761cb46b26539b2e5fce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20=C5=BDoljom?= Date: Thu, 12 Mar 2020 18:04:47 +0100 Subject: [PATCH 3/3] Add tests for config resolver --- tests/Application/ConfigResolverTest.php | 8 +++ .../ComposerWordPress/composer.json | 67 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 tests/Fixtures/ConfigResolver/ComposerWordPress/composer.json diff --git a/tests/Application/ConfigResolverTest.php b/tests/Application/ConfigResolverTest.php index 6629eb80..0d9da813 100644 --- a/tests/Application/ConfigResolverTest.php +++ b/tests/Application/ConfigResolverTest.php @@ -87,6 +87,14 @@ public function testGuessDrupal(): void self::assertSame('drupal', $preset); } + public function testGuessWordPress(): void + { + $preset = ConfigResolver::guess( + Composer::fromPath($this->baseFixturePath . 'ComposerWordPress' . DIRECTORY_SEPARATOR . 'composer.json') + ); + self::assertSame('wordpress', $preset); + } + public function testResolvedConfigIsCorrectlyMerged(): void { $config = [ diff --git a/tests/Fixtures/ConfigResolver/ComposerWordPress/composer.json b/tests/Fixtures/ConfigResolver/ComposerWordPress/composer.json new file mode 100644 index 00000000..e5ca1010 --- /dev/null +++ b/tests/Fixtures/ConfigResolver/ComposerWordPress/composer.json @@ -0,0 +1,67 @@ +{ + "name": "roots/bedrock", + "type": "project", + "license": "MIT", + "description": "WordPress boilerplate with modern development tools, easier configuration, and an improved folder structure", + "homepage": "https://roots.io/bedrock/", + "authors": [ + { + "name": "Scott Walkinshaw", + "email": "scott.walkinshaw@gmail.com", + "homepage": "https://github.com/swalkinshaw" + }, + { + "name": "Ben Word", + "email": "ben@benword.com", + "homepage": "https://github.com/retlehs" + } + ], + "keywords": [ + "bedrock", "composer", "roots", "wordpress", "wp", "wp-config" + ], + "support": { + "issues": "https://github.com/roots/bedrock/issues", + "forum": "https://discourse.roots.io/category/bedrock" + }, + "repositories": [ + { + "type": "composer", + "url": "https://wpackagist.org" + } + ], + "require": { + "php": ">=7.1", + "composer/installers": "^1.8", + "vlucas/phpdotenv": "^4.1.0", + "oscarotero/env": "^1.2.0", + "roots/wordpress": "5.3.2", + "roots/wp-config": "1.0.0", + "roots/wp-password-bcrypt": "1.0.0" + }, + "require-dev": { + "squizlabs/php_codesniffer": "^3.5.4", + "roave/security-advisories": "dev-master" + }, + "config": { + "optimize-autoloader": true, + "preferred-install": "dist" + }, + "minimum-stability": "dev", + "prefer-stable": true, + "extra": { + "installer-paths": { + "web/app/mu-plugins/{$name}/": ["type:wordpress-muplugin"], + "web/app/plugins/{$name}/": ["type:wordpress-plugin"], + "web/app/themes/{$name}/": ["type:wordpress-theme"] + }, + "wordpress-install-dir": "web/wp" + }, + "scripts": { + "post-root-package-install": [ + "php -r \"copy('.env.example', '.env');\"" + ], + "test": [ + "phpcs" + ] + } +}