Skip to content

Commit 74dff1e

Browse files
authored
Merge pull request #21 from polymarket-php/chore(deps)/rector-php
Chore(deps)/rector-php Added rector-php for proper code quality. - Used highest configuration rules - Added github action for rector
2 parents 9dad8a3 + d6fa701 commit 74dff1e

37 files changed

+374
-330
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ jobs:
4040
run: composer install --prefer-dist --no-progress --no-interaction
4141

4242
- name: Run PHP CS Fixer
43-
run: composer cs-check
43+
run: vendor/bin/php-cs-fixer fix --dry-run --diff

.github/workflows/rector.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Rector
2+
3+
on:
4+
push:
5+
branches: [ main, dev ]
6+
pull_request:
7+
branches: [ main, dev ]
8+
9+
jobs:
10+
rector:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
15+
name: Rector Analysis
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
- name: Setup PHP
22+
uses: shivammathur/setup-php@v2
23+
with:
24+
php-version: '8.4'
25+
coverage: none
26+
tools: composer:v2
27+
28+
- name: Get composer cache directory
29+
id: composer-cache
30+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
31+
32+
- name: Cache dependencies
33+
uses: actions/cache@v4
34+
with:
35+
path: ${{ steps.composer-cache.outputs.dir }}
36+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
37+
restore-keys: ${{ runner.os }}-composer-
38+
39+
- name: Install dependencies
40+
run: composer install --prefer-dist --no-progress --no-interaction
41+
42+
- name: Run Rector
43+
run: vendor/bin/rector process --dry-run

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
composer.lock
33
.phpunit.result.cache
44
.php-cs-fixer.cache
5+
.rector-cache
56
.phpunit.cache
67
.idea/
78
*.log

composer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"pestphp/pest": "^4.1",
2626
"pestphp/pest-plugin-arch": "^4.0",
2727
"phpstan/phpstan": "^2.1",
28+
"rector/rector": "^2.3",
2829
"symfony/var-dumper": "^7.3"
2930
},
3031
"autoload": {
@@ -47,8 +48,14 @@
4748
"test": "pest --parallel",
4849
"test-coverage": "pest --coverage",
4950
"phpstan": "phpstan analyse",
50-
"cs-fix": "php-cs-fixer fix",
51-
"cs-check": "php-cs-fixer fix --dry-run --diff"
51+
"format": [
52+
"php-cs-fixer fix",
53+
"vendor/bin/rector"
54+
],
55+
"format-check": [
56+
"php-cs-fixer fix --dry-run --diff",
57+
"rector process --dry-run"
58+
]
5259
},
5360
"config": {
5461
"sort-packages": true,

examples/gamma.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
require_once __DIR__ . '/../vendor/autoload.php';
66

77
use Danielgnh\PolymarketPhp\Client;
8-
use Danielgnh\PolymarketPhp\Enums\OrderSide;
9-
use Danielgnh\PolymarketPhp\Enums\OrderType;
108
use Danielgnh\PolymarketPhp\Exceptions\PolymarketException;
119

1210
$client = new Client();

rector.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Validation\Rule;
6+
use Rector\Config\RectorConfig;
7+
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
8+
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;
9+
10+
return RectorConfig::configure()
11+
->withPaths([
12+
__DIR__ . '/examples',
13+
__DIR__ . '/src',
14+
__DIR__ . '/tests',
15+
])
16+
->withPhpSets(php81: true)
17+
->withTypeCoverageLevel(37)
18+
->withDeadCodeLevel(50)
19+
->withCodeQualityLevel(71)
20+
->withImportNames(
21+
importShortClasses: false,
22+
removeUnusedImports: true,
23+
)
24+
->withRules([
25+
DeclareStrictTypesRector::class,
26+
])
27+
->withParallel(
28+
timeoutSeconds: 120,
29+
maxNumberOfProcess: 16,
30+
jobSize: 16,
31+
)
32+
->withCache(__DIR__ . '/.rector-cache');

src/Auth/ClobAuthenticator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function deriveApiKey(int $nonce = 0): ApiCredentials
110110
*/
111111
public function generateL1Headers(int $nonce = 0, ?int $timestamp = null): array
112112
{
113-
$timestamp = $timestamp ?? time();
113+
$timestamp ??= time();
114114
$signature = $this->signer->signClobAuth($timestamp, $nonce);
115115

116116
return [
@@ -134,11 +134,11 @@ public function generateL2Headers(
134134
?string $body = null,
135135
?int $timestamp = null
136136
): array {
137-
if ($this->credentials === null) {
137+
if (!$this->credentials instanceof ApiCredentials) {
138138
throw ClobAuthenticationException::notSetup();
139139
}
140140

141-
$timestamp = $timestamp ?? time();
141+
$timestamp ??= time();
142142

143143
$signature = HmacSigner::sign(
144144
(string) $timestamp,
@@ -162,7 +162,7 @@ public function generateL2Headers(
162162
*/
163163
public function hasCredentials(): bool
164164
{
165-
return $this->credentials !== null;
165+
return $this->credentials instanceof ApiCredentials;
166166
}
167167

168168
/**

src/Auth/Signer/Eip712Signer.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
class Eip712Signer
2121
{
22-
private string $privateKey;
22+
private readonly string $privateKey;
2323

2424
private Address $ethAddress;
2525

@@ -28,16 +28,13 @@ public function getAddress(): string
2828
return '0x' . $this->ethAddress->get();
2929
}
3030

31-
private int $chainId;
32-
3331
/**
3432
* @throws SigningException
3533
* @throws ClobAuthenticationException
3634
*/
37-
public function __construct(string $privateKey, int $chainId = 137)
35+
public function __construct(string $privateKey, private readonly int $chainId = 137)
3836
{
3937
$this->privateKey = $this->normalizePrivateKey($privateKey);
40-
$this->chainId = $chainId;
4138

4239
try {
4340
$this->ethAddress = new Address(substr($this->privateKey, 2));
@@ -137,7 +134,6 @@ private function buildTypes(): array
137134
* @param array<string, array<array{name: string, type: string}>> $types
138135
* @param array{address: string, timestamp: string, nonce: int, message: string} $message
139136
*
140-
* @return string
141137
* @throws Exception
142138
*/
143139
private function hashTypedData(array $domain, array $types, array $message): string
@@ -164,11 +160,9 @@ private function hashTypedData(array $domain, array $types, array $message): str
164160
/**
165161
* Hash a struct according to EIP-712.
166162
*
167-
* @param string $typeName
168163
* @param array<array{name: string, type: string}> $types
169164
* @param array<string, mixed> $data
170165
*
171-
* @return string
172166
*
173167
* @throws Exception
174168
*/
@@ -242,7 +236,6 @@ private function encodeValue(string $type, mixed $value): string
242236

243237
/**
244238
* @param string $hash Hash to sign (with 0x prefix)
245-
* @return string
246239
*/
247240
private function signHash(string $hash): string
248241
{

src/Bridge.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@
1919
*/
2020
class Bridge
2121
{
22-
private HttpClientInterface $httpClient;
22+
private readonly HttpClientInterface $httpClient;
2323

24-
/**
25-
* @param Config $config
26-
* @param HttpClientInterface|null $httpClient
27-
*/
2824
public function __construct(
2925
private readonly Config $config,
3026
?HttpClientInterface $httpClient = null
@@ -34,8 +30,6 @@ public function __construct(
3430

3531
/**
3632
* Access deposit operations for generating cross-chain deposit addresses.
37-
*
38-
* @return Deposits
3933
*/
4034
public function deposits(): Deposits
4135
{

src/Client.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class Client
1414
{
15-
private Config $config;
15+
private readonly Config $config;
1616

1717
private ?Gamma $gammaClient = null;
1818

@@ -34,15 +34,15 @@ public function __construct(
3434
) {
3535
$this->config = new Config($apiKey, $options);
3636

37-
if ($gammaHttpClient !== null) {
37+
if ($gammaHttpClient instanceof HttpClientInterface) {
3838
$this->gammaClient = new Gamma($this->config, $gammaHttpClient);
3939
}
4040

41-
if ($clobHttpClient !== null) {
41+
if ($clobHttpClient instanceof HttpClientInterface) {
4242
$this->clobClient = new Clob($this->config, $clobHttpClient);
4343
}
4444

45-
if ($bridgeHttpClient !== null) {
45+
if ($bridgeHttpClient instanceof HttpClientInterface) {
4646
$this->bridgeClient = new Bridge($this->config, $bridgeHttpClient);
4747
}
4848
}
@@ -84,7 +84,7 @@ public function auth(
8484

8585
public function gamma(): Gamma
8686
{
87-
if ($this->gammaClient === null) {
87+
if (!$this->gammaClient instanceof Gamma) {
8888
$this->gammaClient = new Gamma($this->config);
8989
}
9090

@@ -93,7 +93,7 @@ public function gamma(): Gamma
9393

9494
public function clob(): Clob
9595
{
96-
if ($this->clobClient === null) {
96+
if (!$this->clobClient instanceof Clob) {
9797
$this->clobClient = new Clob(
9898
$this->config,
9999
null,
@@ -106,12 +106,10 @@ public function clob(): Clob
106106

107107
/**
108108
* Get Bridge API client for cross-chain deposits.
109-
*
110-
* @return Bridge
111109
*/
112110
public function bridge(): Bridge
113111
{
114-
if ($this->bridgeClient === null) {
112+
if (!$this->bridgeClient instanceof Bridge) {
115113
$this->bridgeClient = new Bridge($this->config);
116114
}
117115

0 commit comments

Comments
 (0)