From d029857b6abb7524efa48272d3658a83c931e71f Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Fri, 7 Apr 2023 15:12:19 -0400 Subject: [PATCH 01/13] Adding phpstan --- .github/workflows/coding-quality.yml | 13 ++++++++ composer.json | 5 +++- configure.php | 45 +++++++++++++++++++++------- phpstan.neon | 17 +++++++++++ 4 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/coding-quality.yml create mode 100644 phpstan.neon diff --git a/.github/workflows/coding-quality.yml b/.github/workflows/coding-quality.yml new file mode 100644 index 0000000..ee22892 --- /dev/null +++ b/.github/workflows/coding-quality.yml @@ -0,0 +1,13 @@ +name: Code Quality + +on: + push: + branches: + - main + pull_request: + schedule: + - cron: '0 0 * * *' + +jobs: + coding-quality: + uses: alleyinteractive/.github/.github/workflows/php-code-quality.yml@main diff --git a/composer.json b/composer.json index 602f9d6..f11191c 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "require-dev": { "alleyinteractive/alley-coding-standards": "^1.0", "mantle-framework/testkit": "^0.10", - "nunomaduro/collision": "^5.0" + "nunomaduro/collision": "^5.0", + "szepeviktor/phpstan-wordpress": "^1.1" }, "config": { "allow-plugins": { @@ -46,9 +47,11 @@ "scripts": { "phpcbf": "phpcbf .", "phpcs": "phpcs .", + "phpstan": "phpstan --memory-limit=512M", "phpunit": "phpunit", "test": [ "@phpcs", + "@phpstan", "@phpunit" ] } diff --git a/configure.php b/configure.php index 0778ef9..2511cca 100644 --- a/configure.php +++ b/configure.php @@ -41,7 +41,7 @@ function writeln( string $line ): void { function run( string $command, string $dir = null ): string { $command = $dir ? "cd {$dir} && {$command}" : $command; - return trim( shell_exec( $command ) ); + return trim( (string) shell_exec( $command ) ); } function str_after( string $subject, string $search ): string { @@ -55,7 +55,7 @@ function str_after( string $subject, string $search ): string { } function slugify( string $subject ): string { - return strtolower( trim( preg_replace( '/[^A-Za-z0-9-]+/', '-', $subject ), '-' ) ); + return strtolower( trim( (string) preg_replace( '/[^A-Za-z0-9-]+/', '-', $subject ), '-' ) ); } function title_case( string $subject ): string { @@ -66,15 +66,23 @@ function ensure_capitalp( string $text ): string { return str_replace( 'Wordpress', 'WordPress', $text ); } +/** + * @param string $file + * @param array $replacements + */ function replace_in_file( string $file, array $replacements ): void { $contents = file_get_contents( $file ); + if ( empty( $contents ) ) { + return; + } + file_put_contents( $file, str_replace( array_keys( $replacements ), array_values( $replacements ), - $contents + $contents, ) ); } @@ -82,6 +90,10 @@ function replace_in_file( string $file, array $replacements ): void { function remove_readme_paragraphs( string $file ): void { $contents = file_get_contents( $file ); + if ( empty( $contents ) ) { + return; + } + file_put_contents( $file, trim( preg_replace( '/.*/s', '', $contents ) ?: $contents ), @@ -92,11 +104,17 @@ function determine_separator( string $path ): string { return str_replace( '/', DIRECTORY_SEPARATOR, $path ); } +/** + * @return array + */ function list_all_files_for_replacement(): array { - return explode( PHP_EOL, run( 'grep -R -l ./ --exclude LICENSE --exclude configure.php --exclude composer.lock --exclude-dir .git --exclude-dir .github --exclude-dir vendor --exclude-dir bin --exclude-dir webpack --exclude-dir modules --exclude-dir .phpcs' ) ); + return explode( PHP_EOL, run( 'grep -R -l ./ --exclude LICENSE --exclude .phpunit.result.cache --exclude-dir node_modules --exclude configure.php --exclude composer.lock --exclude-dir .git --exclude-dir .github --exclude-dir vendor --exclude-dir bin --exclude-dir webpack --exclude-dir modules --exclude-dir .phpcs' ) ); } -function delete_files( string|array $paths ) { +/** + * @param string|array $paths + */ +function delete_files( string|array $paths ): void { if ( ! is_array( $paths ) ) { $paths = [ $paths ]; } @@ -112,8 +130,19 @@ function delete_files( string|array $paths ) { } } +$current_dir = getcwd(); + echo "\nWelcome friend to alleyinteractive/create-php-package! 😀\nLet's setup your PHP package 🚀\n\n"; +if ( ! $current_dir ) { + die( 'Could not determine current directory.' ); +} + +$folder_name = ensure_capitalp( basename( $current_dir ) ); + +$package_name = ask( 'Package name?', str_replace( '_', ' ', title_case( $folder_name ) ) ); +$package_name_slug = slugify( $package_name ); + $git_name = run( 'git config user.name' ); $author_name = ask( 'Author name?', $git_name ); @@ -128,12 +157,6 @@ function delete_files( string|array $paths ) { $vendor_name = ask( 'Vendor name (usually the Github Organization)?', $username_guess ); $vendor_slug = slugify( $vendor_name ); -$current_dir = getcwd(); -$folder_name = ensure_capitalp( basename( $current_dir ) ); - -$package_name = ask( 'Package name?', str_replace( '_', ' ', title_case( $folder_name ) ) ); -$package_name_slug = slugify( $package_name ); - $namespace = ask( 'Package namespace?', title_case( $package_name ) ); $class_name = ask( 'Base class name for package?', title_case( $package_name ) ); diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..88adee0 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,17 @@ +includes: + - vendor/szepeviktor/phpstan-wordpress/extension.neon + +parameters: + # Level 9 is the highest level + level: max + + paths: + - src/ + +# ignoreErrors: +# - '#PHPDoc tag @var#' +# +# excludePaths: +# - ./*/*/FileToBeExcluded.php +# +# checkMissingIterableValueType: false From e25489e899aa9cfa3a6b786e8c1421413c9edcf5 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Fri, 7 Apr 2023 16:59:58 -0400 Subject: [PATCH 02/13] coding -> code --- .github/workflows/coding-quality.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/coding-quality.yml b/.github/workflows/coding-quality.yml index ee22892..3472472 100644 --- a/.github/workflows/coding-quality.yml +++ b/.github/workflows/coding-quality.yml @@ -9,5 +9,5 @@ on: - cron: '0 0 * * *' jobs: - coding-quality: + code-quality: uses: alleyinteractive/.github/.github/workflows/php-code-quality.yml@main From f5b5ce31598608b5cfcd06d0b27ffbcb3e3706ab Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 31 Jul 2024 10:04:55 -0400 Subject: [PATCH 03/13] Give package love and bring inline with base standards for namespace --- composer.json | 10 +++--- phpunit.xml | 31 +++++++++---------- src/class-example-package.php | 2 +- .../ExampleTest.php} | 6 ++-- tests/{class-test-case.php => TestCase.php} | 4 +-- .../ExampleUnitTest.php} | 4 +-- 6 files changed, 29 insertions(+), 28 deletions(-) rename tests/{feature/test-example-test.php => Feature/ExampleTest.php} (60%) rename tests/{class-test-case.php => TestCase.php} (53%) rename tests/{unit/test-example-unit-test.php => Unit/ExampleUnitTest.php} (68%) diff --git a/composer.json b/composer.json index 8211e47..7c5e674 100644 --- a/composer.json +++ b/composer.json @@ -31,13 +31,15 @@ }, "sort-packages": true }, + "autoload-dev": { + "psr-4": { + "Alley\\Create_PHP_Package\\Tests\\": "tests" + } + }, "extra": { "wordpress-autoloader": { "autoload": { - "Create_PHP_Package\\": "src" - }, - "autoload-dev": { - "Create_PHP_Package\\Tests\\": "tests" + "Alley\\Create_PHP_Package\\": "src" } } }, diff --git a/phpunit.xml b/phpunit.xml index 7d8c272..b2fe523 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,18 +1,17 @@ + - - - tests/feature - - - tests/unit - - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + bootstrap="tests/bootstrap.php" + backupGlobals="false" + colors="true" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.2/phpunit.xsd" + cacheDirectory=".phpunit.result.cache"> + + + tests/feature + + + tests/unit + + diff --git a/src/class-example-package.php b/src/class-example-package.php index ca2a9c2..e44ba53 100644 --- a/src/class-example-package.php +++ b/src/class-example-package.php @@ -5,7 +5,7 @@ * @package create-php-package */ -namespace Create_PHP_Package; +namespace Alley\Create_PHP_Package; /** * Example Package diff --git a/tests/feature/test-example-test.php b/tests/Feature/ExampleTest.php similarity index 60% rename from tests/feature/test-example-test.php rename to tests/Feature/ExampleTest.php index 0968f85..f063068 100644 --- a/tests/feature/test-example-test.php +++ b/tests/Feature/ExampleTest.php @@ -1,12 +1,12 @@ assertTrue( true ); $this->assertNotEmpty( home_url() ); diff --git a/tests/class-test-case.php b/tests/TestCase.php similarity index 53% rename from tests/class-test-case.php rename to tests/TestCase.php index ecdfb7a..32a96f6 100644 --- a/tests/class-test-case.php +++ b/tests/TestCase.php @@ -1,11 +1,11 @@ assertTrue(true); } From c575fd62730e6e57c8e6c4b8fe7655e80a24ccbb Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 31 Jul 2024 10:10:34 -0400 Subject: [PATCH 04/13] Adding proper namespace --- Makefile | 2 ++ configure.php | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d9e033f..e652afa 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +MAKEFLAGS += --no-builtin-rules + setup: php ./configure.php diff --git a/configure.php b/configure.php index 726fa86..bd64339 100644 --- a/configure.php +++ b/configure.php @@ -157,7 +157,13 @@ function delete_files( string|array $paths ): void { $vendor_name = ask( 'Vendor name (usually the Github Organization)?', $username_guess ); $vendor_slug = slugify( $vendor_name ); -$namespace = ask( 'Package namespace?', title_case( $package_name ) ); +$is_wordpress_package = confirm( 'Is this a WordPress package?', false ); + +$namespace = ask( + 'Package namespace?', + $is_wordpress_package ? 'Alley\\WP\\' . title_case( $package_name ) : 'Alley\\' . title_case( $package_name ), +); + $class_name = ask( 'Base class name for package?', title_case( $package_name ) ); $description = ask( 'Package description?', "This is my PHP package {$package_name}" ); From 22a888520ab28e697625b5c3963ac7b40cc98039 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 31 Jul 2024 10:12:48 -0400 Subject: [PATCH 05/13] Namespace fix --- configure.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/configure.php b/configure.php index bd64339..d3f8089 100644 --- a/configure.php +++ b/configure.php @@ -190,7 +190,11 @@ function delete_files( string|array $paths ): void { 'A skeleton PHP package geared for WordPress Development' => $description, - 'Create_PHP_Package' => $namespace, + // Escape the namespace used in composer.json. + '"Alley\\Create_PHP_Package\\"' => (string) json_encode( $namespace ), + '"Alley\\Create_PHP_Package\\Tests\\"' => (string) json_encode( $namespace . '\\Tests' ), + 'Alley\\Create_PHP_Package' => $namespace, + 'Example_Package' => $class_name, 'package_name' => $package_name, From e8027d92a46863505c26ff37a05940dc37512f22 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 31 Jul 2024 10:15:07 -0400 Subject: [PATCH 06/13] Change the namespace with composer --- configure.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/configure.php b/configure.php index d3f8089..720caf4 100644 --- a/configure.php +++ b/configure.php @@ -207,6 +207,21 @@ function delete_files( string|array $paths ): void { 'alleyinteractive' => $vendor_slug, ]; +// Patch the Composer.json namespace first before search and replace. +run( + 'composer config extra.wordpress-autoloader.autoload --json \'' . json_encode( [ + $namespace => 'src', + ] ) . '\'', +); + +run( + 'composer config autoload-dev --json \'' . json_encode( [ + 'psr-4' => [ + $namespace . '\\Tests' => 'tests', + ], + ] ) . '\'', +); + foreach ( list_all_files_for_replacement() as $path ) { echo "Updating $path...\n"; replace_in_file( $path, $search_and_replace ); From bb13ad12d53b39987c636f09b017051014648fe5 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 31 Jul 2024 10:24:32 -0400 Subject: [PATCH 07/13] Ensure namespace sticks --- configure.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/configure.php b/configure.php index 720caf4..c386687 100644 --- a/configure.php +++ b/configure.php @@ -190,10 +190,9 @@ function delete_files( string|array $paths ): void { 'A skeleton PHP package geared for WordPress Development' => $description, - // Escape the namespace used in composer.json. - '"Alley\\Create_PHP_Package\\"' => (string) json_encode( $namespace ), - '"Alley\\Create_PHP_Package\\Tests\\"' => (string) json_encode( $namespace . '\\Tests' ), - 'Alley\\Create_PHP_Package' => $namespace, + // Extra slashes are here for composer.json. + 'Alley\\\Create_PHP_Package\\\\' => str_replace( '\\', '\\\\', $namespace ) . '\\\\', + 'Alley\Create_PHP_Package' => $namespace, 'Example_Package' => $class_name, 'package_name' => $package_name, From 45f7fca52dd5600a248c2f8044c637bcd7b4fb1b Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 31 Jul 2024 10:24:37 -0400 Subject: [PATCH 08/13] Ensure namespace sticks --- configure.php | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/configure.php b/configure.php index c386687..3dc9ca9 100644 --- a/configure.php +++ b/configure.php @@ -206,21 +206,6 @@ function delete_files( string|array $paths ): void { 'alleyinteractive' => $vendor_slug, ]; -// Patch the Composer.json namespace first before search and replace. -run( - 'composer config extra.wordpress-autoloader.autoload --json \'' . json_encode( [ - $namespace => 'src', - ] ) . '\'', -); - -run( - 'composer config autoload-dev --json \'' . json_encode( [ - 'psr-4' => [ - $namespace . '\\Tests' => 'tests', - ], - ] ) . '\'', -); - foreach ( list_all_files_for_replacement() as $path ) { echo "Updating $path...\n"; replace_in_file( $path, $search_and_replace ); From a4c5ef8f6264710db569a5dae5ff06a2b8b3c704 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 31 Jul 2024 10:26:31 -0400 Subject: [PATCH 09/13] Fixing phps --- .github/workflows/all-pr-tests.yml | 1 + phpcs.xml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/all-pr-tests.yml b/.github/workflows/all-pr-tests.yml index cfc9046..e900230 100644 --- a/.github/workflows/all-pr-tests.yml +++ b/.github/workflows/all-pr-tests.yml @@ -16,6 +16,7 @@ jobs: timeout-minutes: 10 # Define a matrix of PHP/WordPress versions to test against strategy: + fail-fast: false matrix: php: [8.1, 8.2, 8.3] wordpress: ["latest"] diff --git a/phpcs.xml b/phpcs.xml index fc57e61..f356bdf 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -38,7 +38,7 @@ - + From 7ec8750b2916f68f20c47719a47f00e4df25061c Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 31 Jul 2024 10:29:12 -0400 Subject: [PATCH 10/13] Fix suite test name --- phpunit.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index b2fe523..9d96a8b 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -8,10 +8,10 @@ cacheDirectory=".phpunit.result.cache"> - tests/feature + tests/Feature - tests/unit + tests/Unit From fb032a1db2e90e257f43a588bc274e669f65520b Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 31 Jul 2024 10:31:10 -0400 Subject: [PATCH 11/13] Enable SQLite by default --- .github/workflows/all-pr-tests.yml | 1 + phpunit.xml | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/.github/workflows/all-pr-tests.yml b/.github/workflows/all-pr-tests.yml index e900230..359fe9b 100644 --- a/.github/workflows/all-pr-tests.yml +++ b/.github/workflows/all-pr-tests.yml @@ -41,3 +41,4 @@ jobs: php-version: '${{ matrix.php }}' wordpress-version: '${{ matrix.wordpress }}' skip-wordpress-install: 'true' + skip-services: 'true' # Skip MySQL unless the library is not using SQLite testing. diff --git a/phpunit.xml b/phpunit.xml index 9d96a8b..24f82fc 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -14,4 +14,8 @@ tests/Unit + + + + From 5fcedf2663f5918dfa9919c36b2e92e72725fb41 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 31 Jul 2024 10:32:31 -0400 Subject: [PATCH 12/13] Testing CI From 21d5d9cc9521ef275bedd987a71f89603b4b5205 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 31 Jul 2024 10:35:11 -0400 Subject: [PATCH 13/13] Testing CI