From dda8481af486c46cc735c8d5a0716466f8e9eafa Mon Sep 17 00:00:00 2001 From: Alexander Minkin Date: Fri, 31 Jan 2025 19:51:09 +0300 Subject: [PATCH 01/11] feat: add phpstan for static analysis --- chandler_loader.php | 10 ++++++++ composer.json | 6 +++-- composer.lock | 60 ++++++++++++++++++++++++++++++++++++++++++++- openvkctl | 4 +-- phpstan.neon | 14 +++++++++++ 5 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 chandler_loader.php create mode 100644 phpstan.neon diff --git a/chandler_loader.php b/chandler_loader.php new file mode 100644 index 00000000..7566aecc --- /dev/null +++ b/chandler_loader.php @@ -0,0 +1,10 @@ +#!/usr/bin/env php +ignite(true); diff --git a/composer.json b/composer.json index 1020a4f7..8148e237 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,8 @@ { "scripts": { "fix": "php-cs-fixer fix", - "lint": "php-cs-fixer fix --dry-run --diff --verbose" + "lint": "php-cs-fixer fix --dry-run --diff --verbose", + "analyse": "phpstan analyse --memory-limit 1G" }, "require": { "php": "~7.3||~8.1", @@ -28,6 +29,7 @@ }, "minimum-stability": "beta", "require-dev": { - "friendsofphp/php-cs-fixer": "^3.68" + "friendsofphp/php-cs-fixer": "^3.68", + "phpstan/phpstan": "^2.1" } } diff --git a/composer.lock b/composer.lock index 3e7e2d72..9b27d119 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": "b92d2ddd207f394a31c429c65d1785a7", + "content-hash": "fe88a04383a75cc5c6591abac3128201", "packages": [ { "name": "al/emoji-detector", @@ -3092,6 +3092,64 @@ ], "time": "2025-01-30T17:00:50+00:00" }, + { + "name": "phpstan/phpstan", + "version": "2.1.2", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "7d08f569e582ade182a375c366cbd896eccadd3a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/7d08f569e582ade182a375c366cbd896eccadd3a", + "reference": "7d08f569e582ade182a375c366cbd896eccadd3a", + "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-01-21T14:54:06+00:00" + }, { "name": "psr/event-dispatcher", "version": "1.0.0", diff --git a/openvkctl b/openvkctl index 1ed5f3be..39edfecf 100755 --- a/openvkctl +++ b/openvkctl @@ -7,9 +7,7 @@ namespace openvk; use Symfony\Component\Console\Application; -$_SERVER["HTTP_ACCEPT_LANGUAGE"] = false; -$bootstrap = require(__DIR__ . "/../../../chandler/Bootstrap.php"); -$bootstrap->ignite(true); +require(__DIR__ . "/chandler_loader.php"); $application = new Application(); $application->add(new CLI\RebuildImagesCommand()); diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 00000000..4831b315 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,14 @@ +parameters: + level: 0 + paths: + - CLI + - ServiceAPI + - VKAPI + - Web + - bootstrap.php + - openvkctl + - chandler_loader.php + + bootstrapFiles: + - chandler_loader.php + - bootstrap.php From 918d986cfa24358651f0d71aca7ab838c3e7ed50 Mon Sep 17 00:00:00 2001 From: Alexander Minkin Date: Fri, 31 Jan 2025 19:57:54 +0300 Subject: [PATCH 02/11] ci(actions): add phpstan action --- .github/workflows/analyse.yaml | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 .github/workflows/analyse.yaml diff --git a/.github/workflows/analyse.yaml b/.github/workflows/analyse.yaml new file mode 100644 index 00000000..7dbfeb29 --- /dev/null +++ b/.github/workflows/analyse.yaml @@ -0,0 +1,48 @@ +name: Static analysis + +on: + push: + pull_request: + +jobs: + phpstan: + name: PHPStan + runs-on: ubuntu-20.04 + + # 'push' runs on inner branches, 'pull_request' will run only on outer PRs + if: > + github.event_name == 'push' + || (github.event_name == 'pull_request' + && github.event.pull_request.head.repo.full_name != github.repository) + + permissions: + contents: read + steps: + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "8.2" + extensions: gd, zip, intl, yaml, pdo_mysql, rdkafka, imagick + tools: composer:v2 + coverage: none + + - name: Checkout Chandler + uses: actions/checkout@v4 + with: + repository: openvk/chandler + + - name: Install Chandler dependencies + run: composer install --no-interaction --no-progress --no-suggest --prefer-dist + + - name: Checkout OpenVK + uses: actions/checkout@v4 + with: + path: extensions/available/openvk + + - name: Install OpenVK dependencies + working-directory: extensions/available/openvk + run: composer install --no-interaction --no-progress --no-suggest --prefer-dist + + - name: PHPStan + working-directory: extensions/available/openvk + run: composer run-script analyse From 45e53b2a3ed767c3c22aca9abbdb10279bfdf513 Mon Sep 17 00:00:00 2001 From: Alexander Minkin Date: Fri, 31 Jan 2025 20:28:34 +0300 Subject: [PATCH 03/11] ci(actions): do analysing inside docker container --- .github/workflows/analyse.yaml | 40 ++++++++++++---------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/.github/workflows/analyse.yaml b/.github/workflows/analyse.yaml index 7dbfeb29..e0ae6755 100644 --- a/.github/workflows/analyse.yaml +++ b/.github/workflows/analyse.yaml @@ -15,34 +15,22 @@ jobs: || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) - permissions: - contents: read steps: - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: "8.2" - extensions: gd, zip, intl, yaml, pdo_mysql, rdkafka, imagick - tools: composer:v2 - coverage: none - - - name: Checkout Chandler + - name: Code Checkout uses: actions/checkout@v4 - with: - repository: openvk/chandler - - name: Install Chandler dependencies - run: composer install --no-interaction --no-progress --no-suggest --prefer-dist + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 - - name: Checkout OpenVK - uses: actions/checkout@v4 - with: - path: extensions/available/openvk + - name: Build and start Docker container + working-directory: install/automated/docker + run: | + docker build -t openvk ../../.. -f openvk.Dockerfile - - name: Install OpenVK dependencies - working-directory: extensions/available/openvk - run: composer install --no-interaction --no-progress --no-suggest --prefer-dist - - - name: PHPStan - working-directory: extensions/available/openvk - run: composer run-script analyse + - name: Run Docker container with PHPStan + working-directory: install/automated/docker + run: | + docker container run --rm \ + -v ./chandler.example.yml:/opt/chandler/chandler.yml \ + -v ./openvk.example.yml:/opt/chandler/extensions/available/openvk/openvk.yml \ + openvk vendor/bin/phpstan analyse --memory-limit 1G From 1c4305d44fe8c446f0093cb6268438c594ce4f0d Mon Sep 17 00:00:00 2001 From: Alexander Minkin Date: Sun, 9 Feb 2025 16:38:57 +0300 Subject: [PATCH 04/11] fix(FetchToncoinTransactions): add var declaration --- CLI/FetchToncoinTransactions.php | 1 + 1 file changed, 1 insertion(+) diff --git a/CLI/FetchToncoinTransactions.php b/CLI/FetchToncoinTransactions.php index 9de2c998..8342197f 100755 --- a/CLI/FetchToncoinTransactions.php +++ b/CLI/FetchToncoinTransactions.php @@ -18,6 +18,7 @@ define("NANOTON", 1000000000); class FetchToncoinTransactions extends Command { private $images; + private $transactions; protected static $defaultName = "fetch-ton"; From 4ecae188532949b05364db9fefadc8a4a5652a5a Mon Sep 17 00:00:00 2001 From: Alexander Minkin Date: Sun, 9 Feb 2025 16:40:47 +0300 Subject: [PATCH 05/11] fix(ServiceAPI/Wall): add var declaration --- ServiceAPI/Wall.php | 1 + 1 file changed, 1 insertion(+) diff --git a/ServiceAPI/Wall.php b/ServiceAPI/Wall.php index 4066d4f3..db6c32b6 100644 --- a/ServiceAPI/Wall.php +++ b/ServiceAPI/Wall.php @@ -13,6 +13,7 @@ class Wall implements Handler protected $user; protected $posts; protected $notes; + protected $videos; public function __construct(?User $user) { From 059950bbe6b6a7c2c85462fae573910226675d85 Mon Sep 17 00:00:00 2001 From: Alexander Minkin Date: Sun, 9 Feb 2025 16:43:25 +0300 Subject: [PATCH 06/11] fix(bootstrap): remove case-insensitive false vars --- bootstrap.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bootstrap.php b/bootstrap.php index 42241ca4..3fac8214 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -478,8 +478,8 @@ return (function () { define('YEAR', 365 * DAY); define("nullptr", null); - define("OPENVK_DEFAULT_INSTANCE_NAME", "OpenVK", false); - define("OPENVK_VERSION", "Altair Preview ($ver)", false); - define("OPENVK_DEFAULT_PER_PAGE", 10, false); - define("__OPENVK_ERROR_CLOCK_IN_FUTURE", "Server clock error: FK1200-DTF", false); + define("OPENVK_DEFAULT_INSTANCE_NAME", "OpenVK"); + define("OPENVK_VERSION", "Altair Preview ($ver)"); + define("OPENVK_DEFAULT_PER_PAGE", 10); + define("__OPENVK_ERROR_CLOCK_IN_FUTURE", "Server clock error: FK1200-DTF"); }); From b74775fcf5b97159439f2c1d7809b7ac149ece04 Mon Sep 17 00:00:00 2001 From: Alexander Minkin Date: Sun, 9 Feb 2025 16:54:26 +0300 Subject: [PATCH 07/11] fix(VKAPI/Handlers/Board): change parameters order --- VKAPI/Handlers/Board.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/VKAPI/Handlers/Board.php b/VKAPI/Handlers/Board.php index f2f3ec76..28328586 100644 --- a/VKAPI/Handlers/Board.php +++ b/VKAPI/Handlers/Board.php @@ -248,8 +248,9 @@ final class Board extends VKAPIRequestHandler return 1; } - public function editComment(int $comment_id, int $group_id = 0, int $topic_id = 0, string $message, string $attachments) + public function editComment(string $message, string $attachments, int $comment_id, int $group_id = 0, int $topic_id = 0) { + # FIXME /* $this->requireUser(); $this->willExecuteWriteAction(); From ac87ab52e69345886145c57e2607aab73987cbd2 Mon Sep 17 00:00:00 2001 From: Alexander Minkin Date: Sun, 9 Feb 2025 17:01:04 +0300 Subject: [PATCH 08/11] fix(VKAPIRequestHandler): set fail's return type as never --- VKAPI/Handlers/VKAPIRequestHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VKAPI/Handlers/VKAPIRequestHandler.php b/VKAPI/Handlers/VKAPIRequestHandler.php index b67c4797..4804a8dd 100644 --- a/VKAPI/Handlers/VKAPIRequestHandler.php +++ b/VKAPI/Handlers/VKAPIRequestHandler.php @@ -20,7 +20,7 @@ abstract class VKAPIRequestHandler $this->platform = $platform; } - protected function fail(int $code, string $message): void + protected function fail(int $code, string $message): never { throw new APIErrorException($message, $code); } From 87c7c2ec76bb191962a141e21802598720d896cd Mon Sep 17 00:00:00 2001 From: Alexander Minkin Date: Sun, 9 Feb 2025 17:11:07 +0300 Subject: [PATCH 09/11] fix(VKAPI/Handlers/Groups): add array declaration --- VKAPI/Handlers/Groups.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VKAPI/Handlers/Groups.php b/VKAPI/Handlers/Groups.php index baf78210..28ccf11a 100644 --- a/VKAPI/Handlers/Groups.php +++ b/VKAPI/Handlers/Groups.php @@ -45,7 +45,7 @@ final class Groups extends VKAPIRequestHandler $clbsCount = $user->getClubCount(); } - $rClubs; + $rClubs = array(); $ic = sizeof($clbs); if (sizeof($clbs) > $count) { From a1f5fcbe889c2003af29e6ce94363c41484de007 Mon Sep 17 00:00:00 2001 From: Alexander Minkin Date: Sun, 9 Feb 2025 17:20:59 +0300 Subject: [PATCH 10/11] fix(VKAPI/Handlers/Newsfeed): add return_banned declaration --- VKAPI/Handlers/Newsfeed.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VKAPI/Handlers/Newsfeed.php b/VKAPI/Handlers/Newsfeed.php index 346d5312..334fe71b 100644 --- a/VKAPI/Handlers/Newsfeed.php +++ b/VKAPI/Handlers/Newsfeed.php @@ -52,7 +52,7 @@ final class Newsfeed extends VKAPIRequestHandler return $response; } - public function getGlobal(string $fields = "", int $start_from = 0, int $start_time = 0, int $end_time = 0, int $offset = 0, int $count = 30, int $extended = 0, int $rss = 0) + public function getGlobal(string $fields = "", int $start_from = 0, int $start_time = 0, int $end_time = 0, int $offset = 0, int $count = 30, int $extended = 0, int $rss = 0, int $return_banned = 0) { $this->requireUser(); From f672e5001a29b2a5ff5dabd2f75521da05841cf4 Mon Sep 17 00:00:00 2001 From: Alexander Minkin Date: Sun, 9 Feb 2025 17:27:51 +0300 Subject: [PATCH 11/11] fix(VKAPI/Handlers/Notes): move $nodez declaration up --- VKAPI/Handlers/Notes.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/VKAPI/Handlers/Notes.php b/VKAPI/Handlers/Notes.php index cdf5aa45..bde9df75 100644 --- a/VKAPI/Handlers/Notes.php +++ b/VKAPI/Handlers/Notes.php @@ -185,12 +185,14 @@ final class Notes extends VKAPIRequestHandler $this->fail(15, "Access denied"); } + $nodez = (object) [ + "count" => 0, + "notes" => [], + ]; if (empty($note_ids)) { + $nodez->count = (new NotesRepo())->getUserNotesCount($user); + $notes = array_slice(iterator_to_array((new NotesRepo())->getUserNotes($user, 1, $count + $offset, $sort == 0 ? "ASC" : "DESC")), $offset); - $nodez = (object) [ - "count" => (new NotesRepo())->getUserNotesCount((new UsersRepo())->get($user_id)), - "notes" => [], - ]; foreach ($notes as $note) { if ($note->isDeleted()) { @@ -210,6 +212,7 @@ final class Notes extends VKAPIRequestHandler $note = (new NotesRepo())->getNoteById((int) $id[0], (int) $id[1]); if ($note && !$note->isDeleted()) { $nodez->notes[] = $note->toVkApiStruct(); + $nodez->count++; } } }