Skip to content

Commit 62995f7

Browse files
authored
Merge pull request #236 from wpengine/chore-fix-php-linting-errors-previews
chore: Fix PHP linting errors for previews plugin and setup automated code quality
2 parents 003b166 + d0fda91 commit 62995f7

File tree

20 files changed

+405
-201
lines changed

20 files changed

+405
-201
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: 'PHP Code Quality'
2+
description: 'Sets up PHP and runs code quality tools'
3+
inputs:
4+
working-directory:
5+
description: 'Directory to run composer and quality tools in'
6+
required: true
7+
runs:
8+
using: "composite"
9+
steps:
10+
- name: Setup PHP
11+
uses: shivammathur/setup-php@v2
12+
with:
13+
php-version: '7.4'
14+
tools: composer:v2
15+
coverage: none
16+
17+
- name: Install dependencies
18+
uses: ramsey/composer-install@v2
19+
with:
20+
working-directory: ${{ inputs.working-directory }}
21+
composer-options: "--no-progress"
22+
23+
- name: Run PHPStan
24+
working-directory: ${{ inputs.working-directory }}
25+
run: composer run-script phpstan
26+
shell: bash
27+
28+
- name: Run Psalm
29+
working-directory: ${{ inputs.working-directory }}
30+
run: composer run-script php:psalm
31+
shell: bash
32+
33+
- name: Run PHP CodeSniffer
34+
working-directory: ${{ inputs.working-directory }}
35+
run: composer run-script check-cs
36+
shell: bash

.github/workflows/code-quality.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Code Quality
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- 'plugins/**'
7+
8+
jobs:
9+
run:
10+
runs-on: ubuntu-latest
11+
name: Check code quality
12+
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
17+
- name: Get changed plugin directory
18+
id: plugin
19+
run: |
20+
git fetch --prune --unshallow
21+
plugin=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep '^plugins/' | head -1 | cut -d/ -f2)
22+
echo "slug=$plugin" >> $GITHUB_OUTPUT
23+
24+
- name: PHP Code Quality
25+
uses: ./.github/actions/code-quality
26+
with:
27+
working-directory: plugins/${{ steps.plugin.outputs.slug }}

plugins/hwp-previews/ACTIONS_AND_FILTERS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- `hwp_previews_settings_group_settings_group` - Filter to modify the settings group name. Default is HWP_PREVIEWS_SETTINGS_GROUP
1717
- `hwp_previews_settings_group_settings_config` - Filter to modify the settings array. See `Settings_Group`
1818
- `hwp_previews_settings_group_cache_groups` - Filter to modify cache groups for `Settings_Group`
19-
- `hwp_preview_get_post_types_config` - Filter for generating the instance of `Post_Types_Config_Interface`
19+
- `hwp_previews_get_post_types_config` - Filter for generating the instance of `Post_Types_Config_Interface`
2020
- `hwp_previews_hooks_post_type_config` - Filter for post type config service for the Hook class
2121
- `hwp_previews_hooks_post_status_config` - Filter for post status config service for the Hook class
2222
- `hwp_previews_hooks_preview_link_service` - Filter for preview link service for the Hook class

plugins/hwp-previews/composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
}
5151
},
5252
"archive": {
53-
"name": "hwp-previews",
53+
"name": "hwp-previews",
5454
"exclude": [
5555
"/.*",
5656
"bin",
@@ -100,9 +100,9 @@
100100
"phpstan": [
101101
"vendor/bin/phpstan analyze --ansi --memory-limit=1G"
102102
],
103-
"psalm": [
104-
"vendor/bin/psalm --no-progress --show-info=false"
105-
]
103+
"php:psalm": "psalm",
104+
"php:psalm:info": "psalm --show-info=true",
105+
"php:psalm:fix": "psalm --alter"
106106
},
107107
"scripts-descriptions": {
108108
},

plugins/hwp-previews/composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/hwp-previews/hwp-previews.php

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
* @package HWP\Previews
2020
*/
2121

22-
declare( strict_types=1 );
22+
declare(strict_types=1);
2323

24+
use HWP\Previews\Autoloader;
2425
use HWP\Previews\Plugin;
2526

2627
if ( ! defined( 'ABSPATH' ) ) {
@@ -29,62 +30,43 @@
2930

3031
// Load the autoloader.
3132
require_once __DIR__ . '/src/Autoloader.php';
32-
if ( ! \HWP\Previews\Autoloader::autoload() ) {
33+
if ( ! Autoloader::autoload() ) {
3334
return;
3435
}
3536

3637
if ( file_exists( __DIR__ . '/activation.php' ) ) {
3738
require_once __DIR__ . '/activation.php';
39+
// @phpstan-ignore-next-line
3840
register_activation_hook( __FILE__, 'hwp_previews_activation_callback' );
3941
}
4042

4143
if ( file_exists( __DIR__ . '/deactivation.php' ) ) {
4244
require_once __DIR__ . '/deactivation.php';
45+
// @phpstan-ignore-next-line
4346
register_deactivation_hook( __FILE__, 'hwp_previews_deactivation_callback' );
4447
}
4548

4649
/**
4750
* Define plugin constants.
4851
*/
4952
function hwp_previews_constants(): void {
50-
// Plugin version.
51-
if ( ! defined( 'HWP_PREVIEWS_VERSION' ) ) {
52-
define( 'HWP_PREVIEWS_VERSION', '0.0.1' );
53-
}
54-
55-
// Plugin Folder Path.
56-
if ( ! defined( 'HWP_PREVIEWS_PLUGIN_DIR' ) ) {
57-
define( 'HWP_PREVIEWS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
58-
}
59-
60-
// Plugin Folder URL.
61-
if ( ! defined( 'HWP_PREVIEWS_PLUGIN_URL' ) ) {
62-
define( 'HWP_PREVIEWS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
63-
}
64-
65-
// Plugin Root File.
66-
if ( ! defined( 'HWP_PREVIEWS_PLUGIN_FILE' ) ) {
67-
define( 'HWP_PREVIEWS_PLUGIN_FILE', __FILE__ );
68-
}
69-
70-
// Whether to autoload the files or not.
71-
if ( ! defined( 'HWP_PREVIEWS_AUTOLOAD' ) ) {
72-
define( 'HWP_PREVIEWS_AUTOLOAD', true );
73-
}
74-
75-
// Text Domain
76-
if ( ! defined( 'HWP_PREVIEWS_TEXT_DOMAIN' ) ) {
77-
define( 'HWP_PREVIEWS_TEXT_DOMAIN', 'hwp-previews' );
78-
}
79-
80-
// Plugin config settings group
81-
if ( ! defined( 'HWP_PREVIEWS_SETTINGS_GROUP' ) ) {
82-
define( 'HWP_PREVIEWS_SETTINGS_GROUP', 'hwp_previews_settings_group' );
83-
}
84-
85-
// Plugin config settings key.
86-
if ( ! defined( 'HWP_PREVIEWS_SETTINGS_KEY' ) ) {
87-
define( 'HWP_PREVIEWS_SETTINGS_KEY', 'hwp_previews_settings' );
53+
$constants = [
54+
'HWP_PREVIEWS_VERSION' => '0.0.1',
55+
'HWP_PREVIEWS_PLUGIN_DIR' => plugin_dir_path( __FILE__ ),
56+
'HWP_PREVIEWS_PLUGIN_URL' => plugin_dir_url( __FILE__ ),
57+
'HWP_PREVIEWS_PLUGIN_FILE' => __FILE__,
58+
'HWP_PREVIEWS_AUTOLOAD' => true,
59+
'HWP_PREVIEWS_SETTINGS_GROUP' => 'hwp_previews_settings_group',
60+
'HWP_PREVIEWS_SETTINGS_KEY' => 'hwp_previews_settings',
61+
'HWP_PREVIEWS_TEXT_DOMAIN' => 'hwp-previews',
62+
];
63+
64+
foreach ( $constants as $name => $value ) {
65+
if ( ! defined( $name ) ) {
66+
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.VariableConstantNameFound
67+
define( $name, $value );
68+
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.VariableConstantNameFound
69+
}
8870
}
8971

9072
// Plugin Template Directory.
@@ -99,7 +81,6 @@ function hwp_previews_constants(): void {
9981
function hwp_previews_init(): void {
10082
hwp_previews_constants();
10183

102-
10384
if ( defined( 'HWP_PREVIEWS_PLUGIN_DIR' ) ) {
10485
require_once HWP_PREVIEWS_PLUGIN_DIR . 'src/Plugin.php';
10586
Plugin::instance();
@@ -110,14 +91,12 @@ function hwp_previews_init(): void {
11091

11192
add_action(
11293
'admin_notices',
113-
static function () {
94+
static function (): void {
11495
?>
11596
<div class="error notice">
11697
<p>
11798
<?php
118-
printf(
119-
esc_html__( 'Composer vendor directory must be present for HWP Previews to work.', 'hwp-previews' ),
120-
);
99+
echo 'Composer vendor directory must be present for HWP Previews to work.'
121100
?>
122101
</p>
123102
</div>
@@ -128,5 +107,14 @@ static function () {
128107
);
129108
}
130109

110+
/**
111+
* Load plugin textdomain.
112+
*/
113+
function hwp_previews_load_textdomain(): void {
114+
load_plugin_textdomain( 'hwp-previews', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
115+
}
116+
117+
add_action( 'init', 'hwp_previews_load_textdomain', 1, 0 );
118+
131119
/** @psalm-suppress HookNotFound */
132-
add_action( 'plugins_loaded', 'hwp_previews_init', 15 );
120+
add_action( 'plugins_loaded', 'hwp_previews_init', 15, 0 );

plugins/hwp-previews/phpstan.neon.dist

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ parameters:
2929
max: 80400
3030
paths:
3131
- hwp-previews.php
32-
- vendor/autoload.php
3332
- src/
3433
ignoreErrors:
34+
- identifier: empty.notAllowed
3535
-
36-
identifier: empty.notAllowed
36+
message: '#Constant HWP_PREVIEWS.* not found\.#'
37+

plugins/hwp-previews/psalm.xml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,26 @@
99
findUnusedCode="false"
1010
>
1111
<projectFiles>
12-
<directory name="src" />
13-
<file name="hwp-previews.php" />
14-
<file name="vendor/autoload.php" />
12+
<file name="hwp-previews.php"/>
13+
<file name="vendor/autoload.php"/>
14+
<directory name="src"/>
1515
</projectFiles>
1616

1717
<plugins>
18-
<pluginClass class="PsalmWordPress\Plugin" />
18+
<pluginClass class="PsalmWordPress\Plugin"/>
1919
</plugins>
20+
21+
<issueHandlers>
22+
<!-- Noting we had issues with trying to set stubs and also using issueHandlers for particular constants-->
23+
<UndefinedConstant errorLevel="suppress">
24+
<errorLevel type="suppress">
25+
<directory name="src"/>
26+
</errorLevel>
27+
</UndefinedConstant>
28+
<InvalidArgument>
29+
<errorLevel type="suppress">
30+
<file name="src/Hooks/Preview_Hooks.php"/>
31+
</errorLevel>
32+
</InvalidArgument>
33+
</issueHandlers>
2034
</psalm>

0 commit comments

Comments
 (0)