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

Added PHP Static Analysis to dev workflow #1

Merged
merged 3 commits into from
May 25, 2024
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
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ concurrency:

jobs:

# Static Code Analysis (PHPStan)
static-code-analysis:
name: PHPStan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Configure PHP environment
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: mbstring, intl
coverage: none

- uses: ramsey/composer-install@v2

- name: Run PHPStan
run: composer test:analysis

# Execute all PHPUnit tests.
phpunit:
name: PHPUnit (PHP ${{ matrix.php-versions }})
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ composer.lock
.php-cs-fixer.php
phpcs.xml
phpunit.xml
phpstan.neon

# Cache files.
.php_cs.cache
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"erusev/parsedown": "^1.7"
},
"require-dev": {
"phpunit/phpunit": "^9.6"
"phpunit/phpunit": "^9.6",
"phpstan/phpstan": "^1.11"
},
"autoload": {
"psr-4": {
Expand All @@ -29,8 +30,12 @@
"@test:all"
],
"test:all": [
"@test:analysis",
"@test:unit"
],
"test:analysis": [
"./vendor/bin/phpstan analyse -c phpstan.neon.dist --memory-limit=768M"
],
"test:unit": [
"./vendor/bin/phpunit --testdox --verbose --color=always"
]
Expand Down
11 changes: 11 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Configuration for PHPStan
# https://phpstan.org/config-reference

parameters:
level: 6
bootstrapFiles:
- tests/bootstrap-phpstan.php
paths:
- src
- tests

4 changes: 2 additions & 2 deletions src/Parsers/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract class Parser {
/**
* Header map.
*
* @var array
* @var array<string, string>
*/
protected $headerMap = array();

Expand All @@ -23,7 +23,7 @@ abstract class Parser {
*
* @param string $fileContents File contents. Can be safely truncated to 8kiB as that's all WP itself scans.
*
* @return array
* @return array<string, string>
*/
protected function parseHeaders( string $fileContents ) : array {
$headers = array();
Expand Down
8 changes: 4 additions & 4 deletions src/Parsers/PluginParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PluginParser extends Parser {
/**
* Header map.
*
* @var array
* @var array<string, string>
*/
protected $headerMap = array(
'name' => 'Plugin Name',
Expand All @@ -28,9 +28,9 @@ class PluginParser extends Parser {
*
* @param string $content Readme file content.
*
* @return array
* @return null|array<string, string>
*/
public function parseReadme( string $content ) : array {
public function parseReadme( string $content ) : null|array {
$readmeTxtContents = trim( $content, " \t\n\r" );
$readme = array(
'name' => '',
Expand Down Expand Up @@ -149,7 +149,7 @@ public function parseReadme( string $content ) : array {
*
* @param string $fileContents Contents of the plugin file
*
* @return array|null See above for description.
* @return null|array<string, string> See above for description.
*/
public function parsePlugin( string $fileContents ) : array|null {

Expand Down
6 changes: 3 additions & 3 deletions src/Parsers/ThemeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ThemeParser extends Parser {
/**
* Header map.
*
* @var array
* @var array<string, string>
*/
protected $headerMap = array(
'name' => 'Theme Name',
Expand All @@ -30,9 +30,9 @@ class ThemeParser extends Parser {
*
* @param string $fileContents Contents of style.css file.
*
* @return array|null
* @return null|array<string, string>
*/
public function parseStyle( string $fileContents ) : array|null {
public function parseStyle( string $fileContents ) : null|array {

$headers = $this->parseHeaders( $fileContents );
$headers['tags'] = array_filter( array_map( 'trim', explode( ',', strip_tags( $headers['tags'] ) ) ) );
Expand Down
6 changes: 3 additions & 3 deletions src/WPPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class WPPackage {
/**
* Metadata.
*
* @var array
* @var array<string, string>
*/
protected $metadata = array();

Expand Down Expand Up @@ -57,7 +57,7 @@ public function getSlug() : string|null {
/**
* Get metadata.
*
* @return array
* @return array<string, string>
*/
public function getMetaData() : array {
return $this->metadata;
Expand Down Expand Up @@ -150,7 +150,7 @@ public function getType() : string|null {
*
* @param string $file_name File name.
*
* @return bool|array
* @return bool|array<string, string>
*/
private function exploreFile( string $file_name ) : bool|array {
$data = pathinfo( $file_name );
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/Parsers/PluginParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class PluginParserTest extends TestCase {

/**
* Package not found.
*
* @return void
*/
public function test_no_info_when_package_not_found() {
$package = new WPPackage( '/path/wrong/abc.zip' );
Expand All @@ -17,6 +19,8 @@ public function test_no_info_when_package_not_found() {

/**
* Correctly parses a valid plugin.
*
* @return void
*/
public function test_parses_valid_plugin() {
$package = new WPPackage( TESTS_DIR . '/packages/hello-dolly.1.6.zip' );
Expand All @@ -26,6 +30,8 @@ public function test_parses_valid_plugin() {

/**
* getMetaData() should return correct data about the package.
*
* @return void
*/
public function test_getMetaData_should_return_correct_data_for_plugin() {
$package = new WPPackage( TESTS_DIR . '/packages/hello-dolly.1.6.zip' );
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/Parsers/ThemeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class ThemeParserTest extends TestCase {

/**
* Package not found.
*
* @return void
*/
public function test_no_info_when_package_not_found() {
$package = new WPPackage( 'path/wrong/test.zip' );
Expand All @@ -17,6 +19,8 @@ public function test_no_info_when_package_not_found() {

/**
* Correctly parses a valid package.
*
* @return void
*/
public function test_parses_valid_theme() {
$package = new WPPackage( TESTS_DIR . '/packages/twentyseventeen.1.3.zip' );
Expand All @@ -26,6 +30,8 @@ public function test_parses_valid_theme() {

/**
* getMetaData() should return correct data about the package.
*
* @return void
*/
public function test_getMetaData_should_return_correct_data_for_theme() {
$package = new WPPackage( TESTS_DIR . '/packages/twentysixteen.1.3.zip' );
Expand Down
10 changes: 10 additions & 0 deletions tests/bootstrap-phpstan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/**
* Custom bootstrap file for PHPStan.
*
* @link https://phpstan.org/config-reference#bootstrap
*/

define( 'PROJECT_ROOT', dirname( __DIR__ ) );
define( 'TESTS_DIR', dirname( __FILE__ ) );
3 changes: 2 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

error_reporting( E_ALL );
ini_set( 'display_errors', 1 );
define( 'PROJECT_ROOT', dirname( __DIR__ ) );
define( 'TESTS_DIR', dirname( __FILE__ ) );
require_once TESTS_DIR . '/../vendor/autoload.php';
require_once PROJECT_ROOT . '/vendor/autoload.php';
Loading