Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PHP checks #16

Merged
merged 14 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/phpcs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: PHPCS
run-name: Run PHPCS on `${{ github.head_ref }}`

on:
push:
branches:
- master
paths:
- Plugin.php
- includes/**
- .github/workflows/phpcs.yml
pull_request:
paths:
- Plugin.php
- includes/**
- .github/workflows/phpcs.yml
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
phpcs:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v3

- name: Detect File Changes
uses: dorny/paths-filter@v2
id: filter
with:
list-files: shell
filters: |
wpcontent:
- added|modified: 'Plugin.php'
- added|modified: 'includes/**/*.php'

- name: Setup PHP
if: ${{ steps.filter.outputs.wpcontent == 'true' }}
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.1"
ini-values: "memory_limit=1G"
coverage: none

- name: Validate composer.json and composer.lock
if: ${{ steps.filter.outputs.wpcontent == 'true' }}
run: composer validate --no-check-publish

- name: Install Composer dependencies
if: ${{ steps.filter.outputs.wpcontent == 'true' }}
run: |
composer install --prefer-dist --no-progress --no-suggest --optimize-autoloader

- name: Run PHPCS checks
if: ${{ steps.filter.outputs.wpcontent == 'true' }}
run: ./vendor/bin/phpcs ${{ steps.filter.outputs.wpcontent_files }}
59 changes: 59 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: PHPStan
run-name: Run PHPStan on `${{ github.head_ref }}`

on:
push:
branches:
- master
paths:
- Plugin.php
- includes/**
- .github/workflows/phpstan.yml
pull_request:
paths:
- Plugin.php
- includes/**
- .github/workflows/phpstan.yml
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
phpstan:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v3

- name: Detect File Changes
uses: dorny/paths-filter@v2
id: filter
with:
list-files: shell
filters: |
wpcontent:
- added|modified: 'Plugin.php'
- added|modified: 'includes/**/*.php'

- name: Setup PHP
if: ${{ steps.filter.outputs.wpcontent == 'true' }}
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.1"
ini-values: "memory_limit=1G"
coverage: none

- name: Validate composer.json and composer.lock
if: ${{ steps.filter.outputs.wpcontent == 'true' }}
run: composer validate --no-check-publish

- name: Install Composer dependencies
if: ${{ steps.filter.outputs.wpcontent == 'true' }}
run: |
composer install --prefer-dist --no-progress --no-suggest --optimize-autoloader

- name: Run PHPStan checks
if: ${{ steps.filter.outputs.wpcontent == 'true' }}
run: ./vendor/bin/phpstan analyse ${{ steps.filter.outputs.wpcontent_files }}
19 changes: 12 additions & 7 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
name: PHPUnit
run-name: Run PHPUnit on `${{ github.head_ref }}`

on:
push:
branches:
- master
paths:
- Plugin.php
- includes/**
- tests/**
- .github/workflows/phpunit.yml
pull_request:
paths:
- Plugin.php
- includes/**
- tests/**
- .github/workflows/phpunit.yml
workflow_dispatch:

jobs:
Expand All @@ -16,7 +24,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['7.4']
php-versions: ['8.1']
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
services:
mysql:
Expand All @@ -36,7 +44,7 @@ jobs:
WP_MULTISITE: '0'
steps:
- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -45,15 +53,13 @@ jobs:
ini-values: post_max_size=256M
coverage: xdebug #optional, setup coverage driver
extensions: mysqli, mbstring, intl
tools: composer:v1

- name: Check PHP Version
run: php -v

- name: Get Composer cache directory
working-directory: ./tests
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Set up Composer caching
uses: actions/[email protected]
Expand All @@ -66,11 +72,10 @@ jobs:
${{ runner.os }}-composer-

- name: Composer install
working-directory: ./tests
run: composer install --optimize-autoloader --prefer-dist --no-suggest --no-progress --no-ansi --no-interaction

- name: Install WP Tests
run: ./bin/install-wp-tests.sh ${MYSQL_DATABASE} ${MYSQL_USER} ${MYSQL_PASSWORD} ${DB_HOST}:${DB_PORT} ${WP_VERSION}

- name: phpunit tests
run: ./tests/vendor/bin/phpunit --coverage-text
run: ./vendor/bin/phpunit --coverage-text
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/report
/tests/vendor
/vendor
/.phpstan-cache
/.phpunit.result.cache
49 changes: 0 additions & 49 deletions .phpcs.xml.dist

This file was deleted.

29 changes: 17 additions & 12 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ final class Plugin {
*
* @return self
*/
static function instance() : self {
public static function instance() : self {
static $instance = null;

if ( is_null( $instance ) )
if ( is_null( $instance ) ) {
$instance = new self; // @codeCoverageIgnore
}

return $instance;
}
Expand All @@ -51,10 +52,8 @@ static function instance() : self {
* @codeCoverageIgnore
*/
protected function __construct() {

add_action( 'template_redirect', array( $this, 'action__template_redirect' ), 100 );
add_action( 'admin_init', array( $this, 'action__admin_init' ) );

}

/**
Expand All @@ -66,11 +65,20 @@ protected function __construct() {
*
* @codeCoverageIgnore
*/
function action__template_redirect() : void {
public function action__template_redirect() : void {
static::include_files();
}

function action__admin_init() : void {
/**
* Action: admin_init
*
* - include files, except for AJAX requests
*
* @uses static::includes()
*
* @codeCoverageIgnore
*/
public function action__admin_init() : void {
if ( ! wp_doing_ajax() ) {
return;
}
Expand All @@ -83,7 +91,7 @@ function action__admin_init() : void {
*
* @return string
*/
static function dir() : string {
public static function dir() : string {
return trailingslashit( __DIR__ );
}

Expand All @@ -93,7 +101,7 @@ static function dir() : string {
* @uses static::dir()
* @return string
*/
static function inc() : string {
public static function inc() : string {
return static::dir() . 'includes/';
}

Expand All @@ -105,7 +113,7 @@ static function inc() : string {
*
* @codeCoverageIgnore
*/
static function include_files() : void {
public static function include_files() : void {
$dir = static::inc();

# Interfaces.
Expand All @@ -126,11 +134,8 @@ static function include_files() : void {

# Base.
require_once $dir . 'types/Image_Tag.php';

}

}

Plugin::instance();

?>
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"require-dev": {
"yoast/phpunit-polyfills": "^1.0",
"phpcompatibility/phpcompatibility-wp": "^2.1",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1",
"automattic/vipwpcs": "^2.3",
"phpunit/phpunit": "^9",
"assertwell/wp-core-test-framework": "^0.2.0",
"phpstan/phpstan": "^1.6",
"php-stubs/wordpress-stubs": "^5.9",
"szepeviktor/phpstan-wordpress": "^1.1",
"phpstan/extension-installer": "^1.1",
"php-stubs/acf-pro-stubs": "^5.12"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
}
}
}
Loading