Skip to content

Commit

Permalink
feat: add linting of code (#1220)
Browse files Browse the repository at this point in the history
* feat(lint): add php-cs-fixer for linting

Removing previous CODE_STYLE as it was not enforced anyway and using PER-CS 2.0.

This is not the reformatting commit.

* style: format code according to PER-CS 2.0 with php-cs-fixer

* ci(actions): add lint action

Resolves #1132.
  • Loading branch information
WerySkok authored Jan 31, 2025
1 parent 228f14e commit 6ec54a3
Show file tree
Hide file tree
Showing 204 changed files with 13,872 additions and 9,928 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Lint

on:
push:
pull_request:

jobs:
lint:
runs-on: ubuntu-20.04
permissions:
contents: read
steps:
- name: Code Checkout
uses: actions/checkout@v4

- 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: Install dependencies
run: composer install --no-interaction --no-progress --no-suggest --prefer-dist

- name: PHP CS Fixer
run: vendor/bin/php-cs-fixer fix --dry-run --diff
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ themepacks/*
storage/*
!storage/.gitkeep

.idea
.idea
.php-cs-fixer.cache
14 changes: 14 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
;

return (new PhpCsFixer\Config())
->setRules([
'@PER-CS2.0' => true,
'@PHP82Migration' => true,
])
->setFinder($finder)
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
;
210 changes: 107 additions & 103 deletions CLI/FetchToncoinTransactions.php
Original file line number Diff line number Diff line change
@@ -1,103 +1,107 @@
<?php declare(strict_types=1);
namespace openvk\CLI;
use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Repositories\Users;
use openvk\Web\Models\Entities\Notifications\CoinsTransferNotification;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Nette\Utils\ImageException;

define("NANOTON", 1000000000);

class FetchToncoinTransactions extends Command
{
private $images;

protected static $defaultName = "fetch-ton";

function __construct()
{
$this->transactions = DatabaseConnection::i()->getContext()->table("cryptotransactions");

parent::__construct();
}

protected function configure(): void
{
$this->setDescription("Fetches TON transactions to top up the users' balance")
->setHelp("This command checks for new transactions on TON Wallet and then top up the balance of specified users");
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$header = $output->section();

$header->writeln([
"TONCOIN Fetcher",
"=====================",
"",
]);

if(!OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["enabled"]) {
$header->writeln("Sorry, but you handn't enabled the TON support in your config file yet.");

return Command::FAILURE;
}

$testnetSubdomain = OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["testnet"] ? "testnet." : "";
$url = "https://" . $testnetSubdomain . "toncenter.com/api/v2/getTransactions?";

$opts = [
"http" => [
"method" => "GET",
"header" => "Accept: application/json"
]
];

$selection = $this->transactions->select('hash, lt')->order("id DESC")->limit(1)->fetch();
$trHash = $selection->hash ?? NULL;
$trLt = $selection->lt ?? NULL;

$data = http_build_query([
"address" => OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["address"],
"limit" => 100,
"hash" => $trHash,
"to_lt" => $trLt
]);

$response = file_get_contents($url . $data, false, stream_context_create($opts));
$response = json_decode($response, true);

$header->writeln("Gonna up the balance of users");
foreach($response["result"] as $transfer) {
$outputArray;
preg_match('/' . OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["regex"] . '/', $transfer["in_msg"]["message"], $outputArray);
$userId = ctype_digit($outputArray[1]) ? intval($outputArray[1]) : NULL;
if(is_null($userId)) {
$header->writeln("Well, that's a donation. Thanks! XD");
} else {
$user = (new Users)->get($userId);
if(!$user) {
$header->writeln("Well, that's a donation. Thanks! XD");
} else {
$value = ($transfer["in_msg"]["value"] / NANOTON) / OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["rate"];
$user->setCoins($user->getCoins() + $value);
$user->save();
(new CoinsTransferNotification($user, (new Users)->get(OPENVK_ROOT_CONF["openvk"]["preferences"]["support"]["adminAccount"]), (int) $value, "Via TON cryptocurrency"))->emit();
$header->writeln($value . " coins are added to " . $user->getId() . " user id");
$this->transactions->insert([
"id" => NULL,
"hash" => $transfer["transaction_id"]["hash"],
"lt" => $transfer["transaction_id"]["lt"]
]);
}
}
}

$header->writeln("Processing finished :3");

return Command::SUCCESS;
}
}
<?php

declare(strict_types=1);

namespace openvk\CLI;

use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Repositories\Users;
use openvk\Web\Models\Entities\Notifications\CoinsTransferNotification;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Nette\Utils\ImageException;

define("NANOTON", 1000000000);

class FetchToncoinTransactions extends Command
{
private $images;

protected static $defaultName = "fetch-ton";

public function __construct()
{
$this->transactions = DatabaseConnection::i()->getContext()->table("cryptotransactions");

parent::__construct();
}

protected function configure(): void
{
$this->setDescription("Fetches TON transactions to top up the users' balance")
->setHelp("This command checks for new transactions on TON Wallet and then top up the balance of specified users");
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$header = $output->section();

$header->writeln([
"TONCOIN Fetcher",
"=====================",
"",
]);

if (!OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["enabled"]) {
$header->writeln("Sorry, but you handn't enabled the TON support in your config file yet.");

return Command::FAILURE;
}

$testnetSubdomain = OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["testnet"] ? "testnet." : "";
$url = "https://" . $testnetSubdomain . "toncenter.com/api/v2/getTransactions?";

$opts = [
"http" => [
"method" => "GET",
"header" => "Accept: application/json",
],
];

$selection = $this->transactions->select('hash, lt')->order("id DESC")->limit(1)->fetch();
$trHash = $selection->hash ?? null;
$trLt = $selection->lt ?? null;

$data = http_build_query([
"address" => OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["address"],
"limit" => 100,
"hash" => $trHash,
"to_lt" => $trLt,
]);

$response = file_get_contents($url . $data, false, stream_context_create($opts));
$response = json_decode($response, true);

$header->writeln("Gonna up the balance of users");
foreach ($response["result"] as $transfer) {
$outputArray;
preg_match('/' . OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["regex"] . '/', $transfer["in_msg"]["message"], $outputArray);
$userId = ctype_digit($outputArray[1]) ? intval($outputArray[1]) : null;
if (is_null($userId)) {
$header->writeln("Well, that's a donation. Thanks! XD");
} else {
$user = (new Users())->get($userId);
if (!$user) {
$header->writeln("Well, that's a donation. Thanks! XD");
} else {
$value = ($transfer["in_msg"]["value"] / NANOTON) / OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["rate"];
$user->setCoins($user->getCoins() + $value);
$user->save();
(new CoinsTransferNotification($user, (new Users())->get(OPENVK_ROOT_CONF["openvk"]["preferences"]["support"]["adminAccount"]), (int) $value, "Via TON cryptocurrency"))->emit();
$header->writeln($value . " coins are added to " . $user->getId() . " user id");
$this->transactions->insert([
"id" => null,
"hash" => $transfer["transaction_id"]["hash"],
"lt" => $transfer["transaction_id"]["lt"],
]);
}
}
}

$header->writeln("Processing finished :3");

return Command::SUCCESS;
}
}
28 changes: 17 additions & 11 deletions CLI/RebuildImagesCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace openvk\CLI;

use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Repositories\Photos;
use Symfony\Component\Console\Command\Command;
Expand All @@ -14,7 +18,7 @@ class RebuildImagesCommand extends Command

protected static $defaultName = "build-images";

function __construct()
public function __construct()
{
$this->images = DatabaseConnection::i()->getContext()->table("photos");

Expand All @@ -40,8 +44,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
]);

$filter = ["deleted" => false];
if($input->getOption("upgrade-only"))
$filter["sizes"] = NULL;
if ($input->getOption("upgrade-only")) {
$filter["sizes"] = null;
}

$selection = $this->images->select("id")->where($filter);
$totalPics = $selection->count();
Expand All @@ -52,24 +57,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$errors = 0;
$count = 0;
$avgTime = NULL;
$avgTime = null;
$begin = new \DateTimeImmutable("now");
foreach($selection as $idHolder) {
foreach ($selection as $idHolder) {
$start = microtime(true);

try {
$photo = (new Photos)->get($idHolder->id);
$photo = (new Photos())->get($idHolder->id);
$photo->getSizes(true, true);
$photo->getDimensions();
} catch(ImageException $ex) {
} catch (ImageException $ex) {
$errors++;
}

$timeConsumed = microtime(true) - $start;
if(!$avgTime)
if (!$avgTime) {
$avgTime = $timeConsumed;
else
} else {
$avgTime = ($avgTime + $timeConsumed) / 2;
}

$eta = $begin->getTimestamp() + ceil($totalPics * $avgTime);
$int = (new \DateTimeImmutable("now"))->diff(new \DateTimeImmutable("@$eta"));
Expand All @@ -83,4 +89,4 @@ protected function execute(InputInterface $input, OutputInterface $output): int

return Command::SUCCESS;
}
}
}
Loading

0 comments on commit 6ec54a3

Please sign in to comment.