Skip to content

Commit

Permalink
Added new phpstan workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
L3RAZ committed Feb 3, 2025
1 parent 3a6c1b6 commit 831bb02
Show file tree
Hide file tree
Showing 7 changed files with 416 additions and 9 deletions.
52 changes: 48 additions & 4 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
- name: Run PHP-CS-Fixer
run: php vendor/bin/php-cs-fixer fix --dry-run --diff --using-cache=no

phpstan:
phpstan2:
name: PHPStan
runs-on: ubuntu-latest
strategy:
Expand All @@ -86,6 +86,53 @@ jobs:
- name: Checkout
uses: actions/[email protected]

- name: Setup PHP 8.1
uses: shivammathur/setup-php@v2
with:
php-version: "8.1"
tools: composer:v2

- run: composer install

- name: Run PrestaShop Docker container
run: docker run -d --name prestashop-container prestashop/prestashop:${{ matrix.presta-versions }}

- name: Wait for PrestaShop to be ready
run: |
while ! docker exec prestashop-container curl -sSf http://localhost > /dev/null; do
echo "Waiting for PrestaShop to be ready..."
sleep 5
done
- name: Remove ps_checkout module from PrestaShop container
run: docker exec prestashop-container rm -rf /var/www/html/modules/ps_checkout

- name: Copy module to PrestaShop container
run: docker cp $PWD prestashop-container:/var/www/html/modules/ps_checkout/

- name: Run PHPStan inside PrestaShop container
run: |
docker exec prestashop-container bash -c "cd /var/www/html/modules/ps_checkout && _PS_ROOT_DIR_=/var/www/html php vendor/bin/phpstan analyse -c ./tests/phpstan/phpstan-PS-9.neon --error-format=github"
#
# - name: Stop PrestaShop Docker container
# run: docker stop prestashop-container && docker rm prestashop-container
phpstan:
name: PHPStan
runs-on: ubuntu-latest
strategy:
matrix:
presta-versions: [ "9.0.x-apache" ]
if: contains(github.event.pull_request.labels.*.name, 'ready to review')
steps:
- name: Checkout
uses: actions/[email protected]

- name: Setup PHP 8.1
uses: shivammathur/setup-php@v2
with:
php-version: "8.1"
tools: composer:v2

- name: Cache vendor folder
uses: actions/cache@v1
with:
Expand All @@ -105,9 +152,6 @@ jobs:

- name: Run PHPStan
run: docker run --rm --volumes-from temp-ps -v $PWD:/var/www/html/modules/ps_checkout -e _PS_ROOT_DIR_=/var/www/html/ --workdir=/var/www/html/modules/ps_checkout ghcr.io/phpstan/phpstan:latest-php8.1 analyse --configuration=/var/www/html/modules/ps_checkout/tests/phpstan/phpstan-PS-9.neon --error-format=github

- name: test
run: docker exec temp-ps ls -la /var/www/html/modules/ps_checkout
phpunit:
name: PHPUnit
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"friendsofphp/php-cs-fixer": "^3.68",
"monolog/monolog": "^1.27.1",
"nikic/php-parser": "^4.19.1",
"phpstan/phpstan": "^2.1",
"phpunit/php-code-coverage": "^9.2.31",
"phpunit/phpunit": "~9.6.7",
"prestashop/autoindex": "^2.1",
Expand Down
60 changes: 59 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

240 changes: 236 additions & 4 deletions tests/phpstan/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,238 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

$rootDirectory = getenv('_PS_ROOT_DIR_') ?: __DIR__ . '/../../../../';
$projectDir = __DIR__ . '/../../';
require_once $rootDirectory . 'config/config.inc.php';
require_once $projectDir . 'vendor/autoload.php';
declare(strict_types=1);

function requireFileIfItExists(string $filepath): bool
{
if (file_exists($filepath)) {
require_once $filepath;

return true;
}

return false;
}

$rootDir = getenv('_PS_ROOT_DIR_');
if (!$rootDir) {
echo '[ERROR] Define _PS_ROOT_DIR_ with the path to PrestaShop folder' . PHP_EOL;
exit(1);
}
if (!realpath($rootDir)) {
echo sprintf('[ERROR] _PS_ROOT_DIR_ configuration is wrong. No directory found at %s .', $rootDir) . PHP_EOL;
exit(1);
}

// This file will be in the directory vendor/prestashop/php-dev-tools/phpstan.
$pathToModuleRoot ='/var/www/html/modules/ps_checkout/';

// Add module composer autoloader
require_once realpath('/var/www/html/modules/ps_checkout/vendor/autoload.php');

// Add PrestaShop composer autoload
define('_PS_ADMIN_DIR_', $rootDir . '/admin-dev/');
define('PS_ADMIN_DIR', _PS_ADMIN_DIR_);
define('__PS_BASE_URI__', '/');
define('_THEME_NAME_', 'default-bootstrap');

requireFileIfItExists($rootDir . '/tools/smarty/Smarty.class.php');
requireFileIfItExists($rootDir . '/config/defines.inc.php');
requireFileIfItExists($rootDir . '/config/autoload.php');
requireFileIfItExists($rootDir . '/config/defines_uri.inc.php');
requireFileIfItExists($rootDir . '/config/bootstrap.php');

// Make sure loader php-parser is coming from php stan composer

// 1- Use module vendors
$loader = new \Composer\Autoload\ClassLoader();
$loader->setPsr4('PhpParser\\', ['vendor/nikic/php-parser/lib/PhpParser']);
$loader->register(true);
// 2- Use with Docker container
$loader = new \Composer\Autoload\ClassLoader();
$loader->setPsr4('PhpParser\\', ['/composer/vendor/nikic/php-parser/lib/PhpParser']);
$loader->register(true);
// 3- Use with PHPStan phar
$loader = new \Composer\Autoload\ClassLoader();
// Contains the vendor in phar, like "phar://phpstan.phar/vendor"
$loader->setPsr4('PhpParser\\', ['phar://' . dirname($_SERVER['PATH_TRANSLATED']) . '/../phpstan/phpstan-shim/phpstan.phar/vendor/nikic/php-parser/lib/PhpParser/']);
$loader->register(true);
// 4- Use phpstan phar with sym link
$loader = new \Composer\Autoload\ClassLoader();
$loader->setPsr4('PhpParser\\', ['phar://' . realpath($_SERVER['PATH_TRANSLATED']) . '/vendor/nikic/php-parser/lib/PhpParser/']);
$loader->register(true);

/*
* At this time if _PS_VERSION_ is still undefined, this is likely because
* - we are on a old PrestaShop (below 1.7.0.0),
* - and the shop hasn't been installed yet.
*
* In that case, the constant can be set from another file in the installation folder.
*/
if (!defined('_PS_VERSION_')) {
$legacyInstallationFileDefiningConstant = [
'/install-dev/install_version.php',
'/install/install_version.php',
];
foreach ($legacyInstallationFileDefiningConstant as $file) {
if (requireFileIfItExists($rootDir . $file)) {
define('_PS_VERSION_', _PS_INSTALL_VERSION_);
break;
}
}
}

/*
* Display version of PrestaShop, useful for debug
*/
if (defined('_PS_VERSION_')) {
echo 'Detected PS version ' . _PS_VERSION_ . PHP_EOL;
}

// We must declare these constant in this boostrap script.
// Ignoring the error partern with this value will throw another error if not found
// during the checks.
$constantsToDefine = [
'_DB_SERVER_' => [
'type' => 'string',
],
'_DB_NAME_' => [
'type' => 'string',
],
'_DB_USER_' => [
'type' => 'string',
],
'_DB_PASSWD_' => [
'type' => 'string',
],
'_MYSQL_ENGINE_' => [
'type' => 'string',
],
'_COOKIE_KEY_' => [
'type' => 'string',
],
'_COOKIE_IV_' => [
'type' => 'string',
],
'_DB_PREFIX_' => [
'type' => 'string',
],
'_PS_SSL_PORT_' => [
'type' => 'int',
],
'_THEME_COL_DIR_' => [
'type' => 'string',
],
'_PARENT_THEME_NAME_' => [
'type' => 'string',
],
'_PS_PRICE_DISPLAY_PRECISION_' => [
'type' => 'int',
],
'_PS_PRICE_COMPUTE_PRECISION_' => [
'type' => 'string',
'from' => '1.6.0.11',
],
'_PS_OS_CHEQUE_' => [
'type' => 'int',
],
'_PS_OS_PAYMENT_' => [
'type' => 'int',
],
'_PS_OS_PREPARATION_' => [
'type' => 'int',
],
'_PS_OS_SHIPPING_' => [
'type' => 'int',
],
'_PS_OS_DELIVERED_' => [
'type' => 'int',
],
'_PS_OS_CANCELED_' => [
'type' => 'int',
],
'_PS_OS_REFUND_' => [
'type' => 'int',
],
'_PS_OS_ERROR_' => [
'type' => 'int',
],
'_PS_OS_OUTOFSTOCK_' => [
'type' => 'int',
],
'_PS_OS_OUTOFSTOCK_PAID_' => [
'type' => 'int',
],
'_PS_OS_OUTOFSTOCK_UNPAID_' => [
'type' => 'int',
],
'_PS_OS_BANKWIRE_' => [
'type' => 'int',
],
'_PS_OS_PAYPAL_' => [
'type' => 'int',
],
'_PS_OS_WS_PAYMENT_' => [
'type' => 'int',
],
'_PS_OS_COD_VALIDATION_' => [
'type' => 'int',
],
'_PS_THEME_DIR_' => [
'type' => 'string',
],
'_PS_BASE_URL_' => [
'type' => 'string',
],
'_MODULE_DIR_' => [
'type' => 'string',
],
];

foreach ($constantsToDefine as $key => $constantDetails) {
// If already defined, continue
if (defined($key)) {
continue;
}

// Some constants exist from a specific version of PrestaShop.
// If the running PS version is below the one that created this constant, we pass.
if (!empty($constantDetails['from']) && defined('_PS_VERSION_') && version_compare(_PS_VERSION_, $constantDetails['from'], '<')) {
continue;
}

switch ($constantDetails['type']) {
case 'string':
define($key, 'DUMMY_VALUE');
break;
case 'int':
define($key, 1);
break;
default:
define($key, 'DUMMY_VALUE');
break;
}
}
Loading

0 comments on commit 831bb02

Please sign in to comment.