Skip to content

Commit c04d073

Browse files
authored
ci: add PHP linting with Laravel Pint (#5)
Add Laravel Pint as a dev dependency and fix all code style violations across the codebase. Add a GitHub Actions workflow that runs Pint in check mode on pushes to main and pull requests targeting main.
1 parent 68f1e34 commit c04d073

96 files changed

Lines changed: 6759 additions & 6247 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/lint.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
lint:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Setup PHP
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: '8.2'
24+
coverage: none
25+
26+
- name: Get Composer cache directory
27+
id: composer-cache
28+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
29+
30+
- name: Cache Composer dependencies
31+
uses: actions/cache@v4
32+
with:
33+
path: ${{ steps.composer-cache.outputs.dir }}
34+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
35+
restore-keys: |
36+
${{ runner.os }}-composer-
37+
38+
- name: Install Composer dependencies
39+
run: composer install --no-progress --prefer-dist
40+
41+
- name: Run Laravel Pint
42+
run: vendor/bin/pint --test

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
},
99
"require-dev": {
1010
"phpunit/phpunit": "^9.6",
11-
"yoast/phpunit-polyfills": "^2.0"
11+
"yoast/phpunit-polyfills": "^2.0",
12+
"laravel/pint": "^1.20"
1213
},
1314
"autoload": {
1415
"psr-4": {

composer.lock

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

escalated.php

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Plugin Name: Escalated
45
* Plugin URI: https://github.com/escalated-dev/escalated-wordpress
@@ -13,53 +14,53 @@
1314
* Requires at least: 6.0
1415
* Requires PHP: 8.1
1516
*/
16-
17-
if ( ! defined( 'ABSPATH' ) ) {
17+
if (! defined('ABSPATH')) {
1818
exit;
1919
}
2020

21-
define( 'ESCALATED_VERSION', '1.0.1' );
22-
define( 'ESCALATED_PLUGIN_FILE', __FILE__ );
23-
define( 'ESCALATED_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
24-
define( 'ESCALATED_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
25-
define( 'ESCALATED_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
21+
define('ESCALATED_VERSION', '1.0.1');
22+
define('ESCALATED_PLUGIN_FILE', __FILE__);
23+
define('ESCALATED_PLUGIN_DIR', plugin_dir_path(__FILE__));
24+
define('ESCALATED_PLUGIN_URL', plugin_dir_url(__FILE__));
25+
define('ESCALATED_PLUGIN_BASENAME', plugin_basename(__FILE__));
2626

27-
require_once ESCALATED_PLUGIN_DIR . 'vendor/autoload.php';
27+
require_once ESCALATED_PLUGIN_DIR.'vendor/autoload.php';
2828

2929
spl_autoload_register(
30-
static function ( string $class ): void {
30+
static function (string $class): void {
3131
$prefix = 'Escalated\\';
3232

33-
if ( strpos( $class, $prefix ) !== 0 ) {
33+
if (strpos($class, $prefix) !== 0) {
3434
return;
3535
}
3636

37-
$relative = substr( $class, strlen( $prefix ) );
38-
$segments = explode( '\\', $relative );
39-
$class_name = array_pop( $segments );
40-
$subdir = ! empty( $segments ) ? implode( '/', $segments ) . '/' : '';
37+
$relative = substr($class, strlen($prefix));
38+
$segments = explode('\\', $relative);
39+
$class_name = array_pop($segments);
40+
$subdir = ! empty($segments) ? implode('/', $segments).'/' : '';
4141

4242
// Prefer PSR-4-style files (e.g. includes/Services/TicketService.php).
43-
$psr4_file = ESCALATED_PLUGIN_DIR . 'includes/' . $subdir . $class_name . '.php';
44-
if ( file_exists( $psr4_file ) ) {
43+
$psr4_file = ESCALATED_PLUGIN_DIR.'includes/'.$subdir.$class_name.'.php';
44+
if (file_exists($psr4_file)) {
4545
require_once $psr4_file;
46+
4647
return;
4748
}
4849

4950
// Fallback for WordPress-style class files (e.g. includes/Api/class-ticket-controller.php).
50-
$normalized = preg_replace( '/([a-z0-9])([A-Z])/', '$1-$2', $class_name );
51-
$normalized = strtolower( str_replace( '_', '-', (string) $normalized ) );
52-
$legacy_file = ESCALATED_PLUGIN_DIR . 'includes/' . $subdir . 'class-' . $normalized . '.php';
51+
$normalized = preg_replace('/([a-z0-9])([A-Z])/', '$1-$2', $class_name);
52+
$normalized = strtolower(str_replace('_', '-', (string) $normalized));
53+
$legacy_file = ESCALATED_PLUGIN_DIR.'includes/'.$subdir.'class-'.$normalized.'.php';
5354

54-
if ( file_exists( $legacy_file ) ) {
55+
if (file_exists($legacy_file)) {
5556
require_once $legacy_file;
5657
}
5758
}
5859
);
5960

60-
register_activation_hook( __FILE__, [ \Escalated\Activator::class, 'activate' ] );
61-
register_deactivation_hook( __FILE__, [ \Escalated\Deactivator::class, 'deactivate' ] );
61+
register_activation_hook(__FILE__, [\Escalated\Activator::class, 'activate']);
62+
register_deactivation_hook(__FILE__, [\Escalated\Deactivator::class, 'deactivate']);
6263

63-
add_action( 'plugins_loaded', function () {
64+
add_action('plugins_loaded', function () {
6465
\Escalated\Escalated::instance()->boot();
65-
} );
66+
});
Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,93 @@
11
<?php
2+
23
namespace Escalated\Admin;
34

45
use Escalated\Models\ApiToken;
56

6-
class Admin_Api_Tokens {
7-
8-
public function __construct() {
9-
add_action( 'admin_init', [ $this, 'handle_actions' ] );
7+
class Admin_Api_Tokens
8+
{
9+
public function __construct()
10+
{
11+
add_action('admin_init', [$this, 'handle_actions']);
1012
}
1113

1214
/**
1315
* Render the API tokens admin page.
1416
*/
15-
public function render(): void {
16-
$tokens = ApiToken::all();
17+
public function render(): void
18+
{
19+
$tokens = ApiToken::all();
1720
$plain_token = null;
18-
$message = isset( $_GET['message'] ) ? sanitize_text_field( wp_unslash( $_GET['message'] ) ) : '';
21+
$message = isset($_GET['message']) ? sanitize_text_field(wp_unslash($_GET['message'])) : '';
1922

2023
// Check transient for newly created plain token.
21-
$token_transient = get_transient( 'escalated_new_token_' . get_current_user_id() );
22-
if ( $token_transient ) {
24+
$token_transient = get_transient('escalated_new_token_'.get_current_user_id());
25+
if ($token_transient) {
2326
$plain_token = $token_transient;
24-
delete_transient( 'escalated_new_token_' . get_current_user_id() );
27+
delete_transient('escalated_new_token_'.get_current_user_id());
2528
}
2629

27-
include ESCALATED_PLUGIN_DIR . 'templates/admin/api-tokens.php';
30+
include ESCALATED_PLUGIN_DIR.'templates/admin/api-tokens.php';
2831
}
2932

3033
/**
3134
* Handle POST actions: create, delete.
3235
*/
33-
public function handle_actions(): void {
34-
if ( ! isset( $_POST['escalated_token_action'] ) ) {
36+
public function handle_actions(): void
37+
{
38+
if (! isset($_POST['escalated_token_action'])) {
3539
return;
3640
}
3741

38-
if ( ! current_user_can( 'escalated_manage_api_tokens' ) ) {
39-
wp_die( esc_html__( 'Permission denied.', 'escalated' ) );
42+
if (! current_user_can('escalated_manage_api_tokens')) {
43+
wp_die(esc_html__('Permission denied.', 'escalated'));
4044
}
4145

42-
$action = sanitize_text_field( wp_unslash( $_POST['escalated_token_action'] ) );
43-
$redirect = admin_url( 'admin.php?page=escalated-api-tokens' );
46+
$action = sanitize_text_field(wp_unslash($_POST['escalated_token_action']));
47+
$redirect = admin_url('admin.php?page=escalated-api-tokens');
4448

45-
switch ( $action ) {
49+
switch ($action) {
4650
case 'create':
47-
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_escalated_nonce'] ?? '' ) ), 'escalated_token_create' ) ) {
48-
wp_die( esc_html__( 'Security check failed.', 'escalated' ) );
51+
if (! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_escalated_nonce'] ?? '')), 'escalated_token_create')) {
52+
wp_die(esc_html__('Security check failed.', 'escalated'));
4953
}
5054

51-
$name = sanitize_text_field( wp_unslash( $_POST['name'] ?? '' ) );
52-
$abilities = isset( $_POST['abilities'] ) ? array_map( 'sanitize_text_field', (array) $_POST['abilities'] ) : [ '*' ];
55+
$name = sanitize_text_field(wp_unslash($_POST['name'] ?? ''));
56+
$abilities = isset($_POST['abilities']) ? array_map('sanitize_text_field', (array) $_POST['abilities']) : ['*'];
5357

5458
$expires_at = null;
55-
if ( ! empty( $_POST['expires_at'] ) ) {
56-
$expires_at = sanitize_text_field( wp_unslash( $_POST['expires_at'] ) );
59+
if (! empty($_POST['expires_at'])) {
60+
$expires_at = sanitize_text_field(wp_unslash($_POST['expires_at']));
5761
}
5862

59-
$result = ApiToken::create_token( get_current_user_id(), $name, $abilities );
63+
$result = ApiToken::create_token(get_current_user_id(), $name, $abilities);
6064

61-
if ( $result ) {
65+
if ($result) {
6266
// Store plain token temporarily so it can be displayed once.
63-
set_transient( 'escalated_new_token_' . get_current_user_id(), $result['token'], 60 );
67+
set_transient('escalated_new_token_'.get_current_user_id(), $result['token'], 60);
6468

65-
if ( $expires_at ) {
66-
ApiToken::update( $result['record']->id, [ 'expires_at' => $expires_at ] );
69+
if ($expires_at) {
70+
ApiToken::update($result['record']->id, ['expires_at' => $expires_at]);
6771
}
6872

69-
$redirect = add_query_arg( 'message', 'created', $redirect );
73+
$redirect = add_query_arg('message', 'created', $redirect);
7074
} else {
71-
$redirect = add_query_arg( 'message', 'error', $redirect );
75+
$redirect = add_query_arg('message', 'error', $redirect);
7276
}
7377
break;
7478

7579
case 'delete':
76-
$id = absint( $_POST['id'] ?? 0 );
77-
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_escalated_nonce'] ?? '' ) ), 'escalated_token_delete_' . $id ) ) {
78-
wp_die( esc_html__( 'Security check failed.', 'escalated' ) );
80+
$id = absint($_POST['id'] ?? 0);
81+
if (! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_escalated_nonce'] ?? '')), 'escalated_token_delete_'.$id)) {
82+
wp_die(esc_html__('Security check failed.', 'escalated'));
7983
}
8084

81-
ApiToken::delete( $id );
82-
$redirect = add_query_arg( 'message', 'deleted', $redirect );
85+
ApiToken::delete($id);
86+
$redirect = add_query_arg('message', 'deleted', $redirect);
8387
break;
8488
}
8589

86-
wp_safe_redirect( $redirect );
90+
wp_safe_redirect($redirect);
8791
exit;
8892
}
8993
}

0 commit comments

Comments
 (0)