From ac99a78bdf3c3662a5f7dcf6662be1f122894d61 Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Fri, 25 Apr 2025 21:27:31 -0400 Subject: [PATCH] static test --- .gitlab/ci/tests.yml | 14 ++++++ composer.json | 6 ++- composer.lock | 60 +++++++++++++++++++++++++- config.php | 9 +++- phpstan.neon | 5 +++ src/Facades/Config.php | 2 +- src/Http/JsonResponseFactory.php | 2 +- src/Http/Middleware/CorsMiddleware.php | 6 +-- src/Server.php | 13 +++--- 9 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 phpstan.neon diff --git a/.gitlab/ci/tests.yml b/.gitlab/ci/tests.yml index dfd8f58..dd32ef7 100644 --- a/.gitlab/ci/tests.yml +++ b/.gitlab/ci/tests.yml @@ -40,3 +40,17 @@ Run Code Lint: image: siteworxpro/composer script: - composer run tests:lint + +Run Code Sniffer: + stage: tests + needs: + - Install Composer Libraries + rules: + - if: '$CI_COMMIT_TAG' + when: never + - if: '$CI_PIPELINE_SOURCE == "push"' + when: on_success + - when: never + image: siteworxpro/composer + script: + - composer run tests:phpstan \ No newline at end of file diff --git a/composer.json b/composer.json index 5ffe650..4dcbab4 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "phpunit/phpunit": "^12.1", "mockery/mockery": "^1.6", "squizlabs/php_codesniffer": "^3.12", - "lendable/composer-license-checker": "^1.2" + "lendable/composer-license-checker": "^1.2", + "phpstan/phpstan": "^2.1" }, "scripts": { "tests:unit": [ @@ -37,6 +38,9 @@ ], "tests:license": [ "composer-license-checker" + ], + "tests:phpstan": [ + "phpstan analyse --level 4 ./src/ -c phpstan.neon" ] }, "repositories": { diff --git a/composer.lock b/composer.lock index 5da173d..ab96a3e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8e6be626529d82f7cdc7631ba7f92165", + "content-hash": "597f3c330fcbd311310b6c23e67f82ad", "packages": [ { "name": "brick/math", @@ -2949,6 +2949,64 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "phpstan/phpstan", + "version": "2.1.12", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "96dde49e967c0c22812bcfa7bda4ff82c09f3b0c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/96dde49e967c0c22812bcfa7bda4ff82c09f3b0c", + "reference": "96dde49e967c0c22812bcfa7bda4ff82c09f3b0c", + "shasum": "" + }, + "require": { + "php": "^7.4|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + } + ], + "time": "2025-04-16T13:19:18+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "12.1.2", diff --git a/config.php b/config.php index 6ea2a8d..2e718ac 100644 --- a/config.php +++ b/config.php @@ -8,7 +8,8 @@ return [ * The server configuration. */ 'server' => [ - 'port' => Env::get('HTTP_PORT', 9501), + 'port' => Env::get('HTTP_PORT', 9501, 'int'), + 'dev_mode' => Env::get('DEV_MODE', false, 'bool'), ], /** @@ -20,5 +21,11 @@ return [ 'database' => Env::get('DB_DATABASE', 'siteworxpro'), 'username' => Env::get('DB_USERNAME', 'siteworxpro'), 'password' => Env::get('DB_PASSWORD', 'password'), + ], + + 'cors' => [ + 'allowed_origins' => Env::get('CORS_ALLOWED_ORIGINS', 'http://localhost:3000'), + 'allow_credentials' => Env::get('CORS_ALLOW_CREDENTIALS', true, 'bool'), + 'max_age' => Env::get('CORS_MAX_AGE', 3600, 'int'), ] ]; \ No newline at end of file diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..88a2424 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + tips: + treatPhpDocTypesAsCertain: false + ignoreErrors: + - '#Static call to instance method#' \ No newline at end of file diff --git a/src/Facades/Config.php b/src/Facades/Config.php index d0ffc57..500e05a 100644 --- a/src/Facades/Config.php +++ b/src/Facades/Config.php @@ -15,7 +15,7 @@ use Siteworx\Config\Exception\UnsupportedFormatException; * This class serves as a facade for the configuration settings of the application. * It extends the Facade class from the Illuminate\Support\Facades namespace. * - * @method static string|int|bool|float get(string $key) Retrieve the configuration value for the given key. + * @method static bool | string | int get(string $key) Retrieve the configuration value for the given key. * * @package Siteworx\App\Facades */ diff --git a/src/Http/JsonResponseFactory.php b/src/Http/JsonResponseFactory.php index 7bdb071..c4f1914 100644 --- a/src/Http/JsonResponseFactory.php +++ b/src/Http/JsonResponseFactory.php @@ -16,7 +16,7 @@ class JsonResponseFactory /** * Create a JSON response with the given data and status code. * - * @param mixed $data The data to include in the response. + * @param array $data The data to include in the response. * @param int $statusCode The HTTP status code for the response. * @return Response The JSON response. * @throws \JsonException diff --git a/src/Http/Middleware/CorsMiddleware.php b/src/Http/Middleware/CorsMiddleware.php index eecca76..3bd7ab7 100644 --- a/src/Http/Middleware/CorsMiddleware.php +++ b/src/Http/Middleware/CorsMiddleware.php @@ -34,7 +34,7 @@ class CorsMiddleware implements MiddlewareInterface 'trim', explode( ',', - Config::get('CORS_ALLOWED_ORIGINS', 'https://example.com,https://another.com') + Config::get('cors.allowed_origins') ) ); @@ -57,11 +57,11 @@ class CorsMiddleware implements MiddlewareInterface ?: 'Content-Type, Authorization' ); - if (Config::get('CORS_ALLOW_CREDENTIALS', 'bool')) { + if (Config::get('cors.allow_credentials') === true) { $response = $response->withHeader('Access-Control-Allow-Credentials', 'true'); } - $maxAge = Config::get('CORS_MAX_AGE') ?: '86400'; + $maxAge = Config::get('CORS_MAX_AGE') !== 3600 ? Config::get('CORS_MAX_AGE') : 3600; return $response->withHeader('Access-Control-Max-Age', $maxAge); } diff --git a/src/Server.php b/src/Server.php index 86210d3..18a0e0b 100644 --- a/src/Server.php +++ b/src/Server.php @@ -16,12 +16,13 @@ use Spiral\RoadRunner\Http\PSR7Worker; use Spiral\RoadRunner\Worker; /** - * Abstract class Server + * Class Server * - * This abstract class serves as a base for creating server instances. - * It initializes the PSR-7 worker and router, and provides an abstract method - * for registering routes. It also includes a method to start the server and handle - * incoming requests. + * This class represents the main server application. + * It handles incoming HTTP requests, routes them to the appropriate handlers, + * and manages the server lifecycle. + * + * @package Siteworxpro\App */ class Server { @@ -153,7 +154,7 @@ class Server Logger::error($e->getTraceAsString()); $json = ['status_code' => 500, 'reason_phrase' => 'Server Error']; - if (Config::get("DEV_MODE", 'bool')) { + if (Config::get("server.dev_mode")) { $json = [ 'status_code' => 500, 'reason_phrase' => 'Server Error',