From 5fe4aac7eb9524bd37e8c72f45b2755d197dea5b Mon Sep 17 00:00:00 2001 From: Nikita Kiselev Date: Mon, 1 Dec 2025 18:04:54 +0300 Subject: [PATCH] refactor: correct static analyze errors --- .github/workflows/main.yaml | 4 ++ Makefile | 2 +- docker-compose.yaml | 2 +- .../bastion/Handlers/StatsHandler.php | 6 ++ .../framework/Cache/DatabaseCache.php | 62 ------------------- .../Cache/SymfonyFilesystemCache.php | 44 ------------- .../framework/Container/Container.php | 5 +- .../Exceptions/ActionNotFoundException.php | 1 + .../framework/ImageTool/ImageTool.php | 4 +- .../Decorators/OcRegistryDecorator.php | 1 + .../upload/oc_telegram_shop/phpstan.neon | 15 +++++ .../Adapters/OcModelCatalogProductAdapter.php | 2 +- .../src/Filters/ProductAttribute.php | 2 +- .../src/Filters/ProductCategories.php | 2 +- .../src/Filters/ProductCategory.php | 2 +- .../src/Filters/ProductManufacturer.php | 2 +- .../src/Filters/ProductModel.php | 2 +- .../src/Filters/ProductPrice.php | 2 +- .../src/Filters/ProductQuantity.php | 2 +- .../src/Filters/ProductStatus.php | 2 +- 20 files changed, 41 insertions(+), 123 deletions(-) delete mode 100755 module/oc_telegram_shop/upload/oc_telegram_shop/framework/Cache/DatabaseCache.php delete mode 100755 module/oc_telegram_shop/upload/oc_telegram_shop/framework/Cache/SymfonyFilesystemCache.php create mode 100644 module/oc_telegram_shop/upload/oc_telegram_shop/phpstan.neon diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index bc14906..2c7a1c1 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -56,6 +56,10 @@ jobs: APP_ENV: testing run: ./vendor/bin/phpunit --testdox tests/Unit tests/Telegram + - name: Static Analyzer + working-directory: module/oc_telegram_shop/upload/oc_telegram_shop + run: ./vendor/bin/phpstan analyse + phpcs: name: Run PHP_CodeSniffer runs-on: ubuntu-latest diff --git a/Makefile b/Makefile index 0fd9208..911aecd 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ dev-admin: cd frontend/admin && npm run dev lint: - docker compose exec -w /module/oc_telegram_shop/upload/oc_telegram_shop web bash -c "./vendor/bin/phpstan analyse src framework" + docker compose exec -w /module/oc_telegram_shop/upload/oc_telegram_shop web bash -c "./vendor/bin/phpstan analyse" phpcs: docker compose exec -w /module/oc_telegram_shop/upload/oc_telegram_shop web bash -c "./vendor/bin/phpcs --standard=PSR12 bastion framework src" diff --git a/docker-compose.yaml b/docker-compose.yaml index e51e15f..57ff888 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -14,7 +14,7 @@ services: deploy: resources: limits: - memory: 512M + memory: 1G healthcheck: test: [ "CMD", "curl" ,"-f", "http://localhost/index.php?route=extension/tgshop/handle&api_action=health" ] interval: 10s diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/bastion/Handlers/StatsHandler.php b/module/oc_telegram_shop/upload/oc_telegram_shop/bastion/Handlers/StatsHandler.php index 15be8dd..72eecfa 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/bastion/Handlers/StatsHandler.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/bastion/Handlers/StatsHandler.php @@ -18,6 +18,12 @@ class StatsHandler public function getDashboardStats(): JsonResponse { + $data = [ + 'orders_count' => 0, + 'orders_total_amount' => 0, + 'customers_count' => 0, + ]; + $ordersTotalAmount = $this->builder->newQuery() ->select([ new RawExpression('COUNT(DISTINCT orders.order_id) AS orders_total_count'), diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Cache/DatabaseCache.php b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Cache/DatabaseCache.php deleted file mode 100755 index f7e827c..0000000 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Cache/DatabaseCache.php +++ /dev/null @@ -1,62 +0,0 @@ -connection = $connection; - $this->table = $table; - } - - public function get(string $key) - { - $cache = $this->connection->select( - " - SELECT result - FROM $this->table - WHERE cache_key = :key AND (expires_at IS NULL OR expires_at > NOW()) - ", - [':key' => $key] - ); - - return $cache ? unserialize($cache[0]['result'], ['allowed_classes' => true]) : null; - } - - public function set(string $key, $value, ?int $ttlSeconds = null): void - { - $this->connection->statement("DELETE FROM $this->table WHERE expires_at IS NOT NULL AND expires_at <= NOW()"); - - $expiresAt = $ttlSeconds ? date('Y-m-d H:i:s', time() + $ttlSeconds) : null; - - $sql = <<table (cache_key, result, expires_at) - VALUES (:key, :result, :expiresAt) - ON DUPLICATE KEY UPDATE - result = VALUES(result), - expires_at = VALUES(expires_at) - SQL; - - $this->connection->statement($sql, [ - ':key' => $key, - ':result' => serialize($value), - ':expiresAt' => $expiresAt, - ]); - } - - public function delete(string $key): void - { - $this->connection->statement("DELETE FROM $this->table WHERE cache_key = :key", [':key' => $key]); - } - - public function clear(): void - { - $this->connection->truncateTable($this->table); - } -} diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Cache/SymfonyFilesystemCache.php b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Cache/SymfonyFilesystemCache.php deleted file mode 100755 index 44644d9..0000000 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Cache/SymfonyFilesystemCache.php +++ /dev/null @@ -1,44 +0,0 @@ -cache = new FilesystemAdapter($namespace, $defaultLifetime, $cacheDir); - } - - public function get(string $key) - { - $item = $this->cache->getItem($key); - - return $item->isHit() ? $item->get() : null; - } - - public function set(string $key, $value, ?int $ttlSeconds = null): void - { - $item = $this->cache->getItem($key); - $item->set($value); - - if ($ttlSeconds) { - $item->expiresAfter($ttlSeconds); - } - - $this->cache->save($item); - } - - public function delete(string $key): void - { - $this->cache->deleteItem($key); - } - - public function clear(): void - { - $this->cache->clear(); - } -} diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Container/Container.php b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Container/Container.php index 54ad4dd..dc82c28 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Container/Container.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Container/Container.php @@ -62,11 +62,8 @@ class Container implements ContainerInterface /** * @template T - * @param string $id + * @param class-string $id * @return T - * @psalm-param class-string|string $id - * @psalm-suppress MoreSpecificImplementedParamType - * */ public function get(string $id) { diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Exceptions/ActionNotFoundException.php b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Exceptions/ActionNotFoundException.php index db578b7..2851afd 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Exceptions/ActionNotFoundException.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/Exceptions/ActionNotFoundException.php @@ -3,6 +3,7 @@ namespace Openguru\OpenCartFramework\Exceptions; use Exception; +use Throwable; class ActionNotFoundException extends Exception implements NonLoggableExceptionInterface { diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/ImageTool/ImageTool.php b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/ImageTool/ImageTool.php index 8b98e68..477dc13 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/ImageTool/ImageTool.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/ImageTool/ImageTool.php @@ -42,7 +42,7 @@ class ImageTool implements ImageToolInterface return null; // безопасность } - $extless = utf8_substr($filename, 0, utf8_strrpos($filename, '.')); + $extless = substr($filename, 0, strrpos($filename, '.')); $imageNew = 'cache/' . $extless . '-' . $width . 'x' . $height . '.' . $format; $fullNewPath = $this->imageDir . $imageNew; @@ -102,7 +102,7 @@ class ImageTool implements ImageToolInterface return null; // безопасность } - $extless = utf8_substr($path, 0, utf8_strrpos($path, '.')); + $extless = substr($path, 0, strrpos($path, '.')); $imageNew = 'cache/' . $extless . '-' . $width . 'x' . $height . '.' . $format; $fullNewPath = $this->imageDir . $imageNew; diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/OpenCart/Decorators/OcRegistryDecorator.php b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/OpenCart/Decorators/OcRegistryDecorator.php index 97eef08..a1e13a0 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/framework/OpenCart/Decorators/OcRegistryDecorator.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/framework/OpenCart/Decorators/OcRegistryDecorator.php @@ -29,6 +29,7 @@ use Url; * @property ModelSettingSetting $model_setting_setting * @property ModelDesignBanner $model_design_banner * @property ModelCatalogCategory $model_catalog_category + * @property Registry $registry */ class OcRegistryDecorator { diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/phpstan.neon b/module/oc_telegram_shop/upload/oc_telegram_shop/phpstan.neon new file mode 100644 index 0000000..1da1883 --- /dev/null +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/phpstan.neon @@ -0,0 +1,15 @@ +parameters: + level: 1 + paths: + - src + - bastion + - framework + + scanDirectories: + - /web/upload/system + + scanFiles: + - /web/upload/config.php + - /web/upload/admin/config.php + + inferPrivatePropertyTypeFromConstructor: true \ No newline at end of file diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Adapters/OcModelCatalogProductAdapter.php b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Adapters/OcModelCatalogProductAdapter.php index 018ce72..6807e55 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Adapters/OcModelCatalogProductAdapter.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Adapters/OcModelCatalogProductAdapter.php @@ -10,7 +10,7 @@ class OcModelCatalogProductAdapter /** @var Proxy|ModelCatalogProduct */ private Proxy $model; - public function __construct(Proxy $model) + public function __construct($model) { $this->model = $model; } diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductAttribute.php b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductAttribute.php index aeebde8..887a97b 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductAttribute.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductAttribute.php @@ -14,7 +14,7 @@ class ProductAttribute extends BaseRule public static function initWithDefaults(): BaseRule { - return new static(static::NAME, [ + return new self(static::NAME, [ 'product_attribute' => new Criterion(static::CRITERIA_OPTION_PRODUCT_ATTRIBUTE, [ 'attribute_id' => null, 'operator' => static::CRITERIA_OPERATOR_CONTAINS, diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductCategories.php b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductCategories.php index 4f2bae8..8745aa1 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductCategories.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductCategories.php @@ -13,7 +13,7 @@ class ProductCategories extends BaseRule public static function initWithDefaults(): BaseRule { - return new static(static::NAME, [ + return new self(static::NAME, [ 'product_category_ids' => new Criterion(static::CRITERIA_OPTION_PRODUCT_CATEGORIES, [ 'operator' => static::CRITERIA_OPERATOR_CONTAINS, 'value' => [], diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductCategory.php b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductCategory.php index 3d5ff3b..3d905c6 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductCategory.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductCategory.php @@ -14,7 +14,7 @@ class ProductCategory extends BaseRule public static function initWithDefaults(): BaseRule { - return new static(static::NAME, [ + return new self(static::NAME, [ 'product_category_id' => new Criterion(static::CRITERIA_OPTION_PRODUCT_CATEGORIES, [ 'operator' => static::CRITERIA_OPERATOR_CONTAINS, 'value' => null, diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductManufacturer.php b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductManufacturer.php index cdd96c6..855ea54 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductManufacturer.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductManufacturer.php @@ -12,7 +12,7 @@ class ProductManufacturer extends BaseRule public static function initWithDefaults(): BaseRule { - return new static(static::NAME, [ + return new self(static::NAME, [ 'product_manufacturer_ids' => new Criterion(static::CRITERIA_OPTION_PRODUCT_MANUFACTURER, [ 'operator' => static::CRITERIA_OPERATOR_CONTAINS, 'value' => [], diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductModel.php b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductModel.php index 4a56538..65638dc 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductModel.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductModel.php @@ -14,7 +14,7 @@ class ProductModel extends BaseRule public static function initWithDefaults(): BaseRule { - return new static(static::NAME, [ + return new self(static::NAME, [ 'product_model' => new Criterion(static::CRITERIA_OPTION_PRODUCT_MODEL, [ 'operator' => static::CRITERIA_OPERATOR_CONTAINS, 'value' => [], diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductPrice.php b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductPrice.php index 7eb7b98..46bfb8f 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductPrice.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductPrice.php @@ -20,7 +20,7 @@ class ProductPrice extends BaseRule public static function initWithDefaults(): BaseRule { - return new static(static::NAME, [ + return new self(static::NAME, [ 'product_price' => new Criterion(static::CRITERIA_OPTION_NUMBER, [ 'operator' => static::CRITERIA_OPERATOR_GREATER_OR_EQUAL, 'value' => [ diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductQuantity.php b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductQuantity.php index ff6b469..7ebba1e 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductQuantity.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductQuantity.php @@ -17,7 +17,7 @@ class ProductQuantity extends BaseRule public static function initWithDefaults(): BaseRule { - return new static(static::NAME, [ + return new self(static::NAME, [ 'product_quantity' => new Criterion(static::CRITERIA_OPTION_NUMBER, [ 'operator' => static::CRITERIA_OPERATOR_GREATER_OR_EQUAL, 'value' => [0, null], diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductStatus.php b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductStatus.php index c7104b5..30d90a0 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductStatus.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/src/Filters/ProductStatus.php @@ -14,7 +14,7 @@ class ProductStatus extends BaseRule public static function initWithDefaults(): BaseRule { - return new static(static::NAME, [ + return new self(static::NAME, [ 'product_status' => new Criterion(static::CRITERIA_OPTION_BOOLEAN, [ 'operator' => static::CRITERIA_OPERATOR_EQUALS, 'value' => true,