Skip to content

Commit dd845fd

Browse files
authored
Merge pull request #30 from MekDrop/add-phpstan
Added phpstan
2 parents 554ee66 + 0aa5461 commit dd845fd

File tree

8 files changed

+36
-24
lines changed

8 files changed

+36
-24
lines changed

.github/workflows/on_pull_request.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ jobs:
3232
run: composer validate --strict --no-check-lock
3333
- name: Install Composer dependencies (with dev)
3434
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader
35+
- name: PHPStan
36+
run: composer phpstan
3537
- name: PHPUnit
3638
run: composer test
3739
- name: Install Composer dependencies (without dev)
@@ -67,4 +69,4 @@ jobs:
6769
run: gh pr merge --auto --squash "$PR_URL"
6870
env:
6971
PR_URL: ${{ github.event.pull_request.html_url }}
70-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
72+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
"xoops"
2929
],
3030
"scripts": {
31-
"test": "phpunit --testdox"
31+
"test": "phpunit --testdox",
32+
"phpstan": "phpstan analyse"
3233
},
3334
"require-dev": {
34-
"phpunit/phpunit": "^12"
35+
"phpunit/phpunit": "^12",
36+
"phpstan/phpstan": "^2.1"
3537
}
3638
}

phpstan.neon

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
parameters:
2+
level: 5
3+
4+
paths:
5+
- src
6+
- tests
7+
8+
excludePaths:
9+
- vendor

src/CriteriaCompo.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ public function getBindData(): array
7676
/**
7777
* @inheritDoc
7878
*
79-
* @return Traversable<string, CriteriaElement>
79+
* @return Traversable<Condition, CriteriaElement>
8080
*
8181
* @noinspection MethodShouldBeFinalInspection
8282
*/
8383
public function getIterator(): Traversable
8484
{
85-
foreach ($this->elements as $item) {
86-
yield $item[1] => $item[0];
85+
foreach ($this->elements as [$criteriaElement, $condition]) {
86+
yield $condition => $criteriaElement;
8787
}
8888
}
8989

@@ -102,7 +102,7 @@ public function render(bool $withBindVariables = false): ?string
102102
if ($first) {
103103
$first = false;
104104
} else {
105-
$ret .= ' ' . ($join instanceof Condition ? $join->value : $join) . ' ';
105+
$ret .= ' ' . $join->value . ' ';
106106
}
107107
$ret .= '(' . $element->render($withBindVariables) . ')';
108108
}

src/CriteriaElement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function __set(string $name, mixed $value): void
7777
public function __isset(string $name): bool
7878
{
7979
return match ($name) {
80-
'order' => $this->getOrder() !== null,
80+
'order' => true,
8181
'sort' => !empty($this->getSort()),
8282
'limit' => !empty($this->getLimit()),
8383
'start' => !empty($this->getStart()),

src/CriteriaItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public function getBindData(): array
219219

220220
private function isEmptyArrayButNotNullComparison(mixed $value): bool
221221
{
222-
return is_array($value) && empty($value) && in_array($this->operator->value, [
222+
return is_array($value) && empty($value) && in_array($this->operator, [
223223
ComparisonOperator::IS_NOT_NULL,
224224
ComparisonOperator::IS_NULL,
225225
ComparisonOperator::BETWEEN,

tests/CriteriaCompoTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Imponeer\Database\Criteria\CriteriaItem;
99
use Imponeer\Database\Criteria\Enum\Condition;
1010
use Imponeer\Database\Criteria\Enum\Order;
11-
use JsonException;
1211
use PHPUnit\Framework\Attributes\DataProvider;
1312
use PHPUnit\Framework\TestCase;
1413
use Random\RandomException;
@@ -31,14 +30,14 @@ final public static function provideCondition(): array
3130
foreach ([$condition, ' ' . $condition . ' ', strtolower($condition), ucfirst(strtolower($condition))] as $conditionVar) {
3231
// first variant for compo
3332
$compo1 = new CriteriaCompo();
34-
$compo1->add(new CriteriaItem(sha1(random_int(PHP_INT_MIN, PHP_INT_MAX))));
35-
$compo1->add(new CriteriaItem(sha1(random_int(PHP_INT_MIN, PHP_INT_MAX))), $conditionVar);
33+
$compo1->add(new CriteriaItem(sha1((string) random_int(PHP_INT_MIN, PHP_INT_MAX))));
34+
$compo1->add(new CriteriaItem(sha1((string) random_int(PHP_INT_MIN, PHP_INT_MAX))), $conditionVar);
3635
$label = sprintf('Two columns with null values and condition %s', $conditionData->name);
3736
$testsVariations[$label] = [$compo1, $conditionVar];
3837

3938
// 2nd variant for compo
40-
$compo2 = new CriteriaCompo(new CriteriaItem(sha1(random_int(PHP_INT_MIN, PHP_INT_MAX))));
41-
$compo2->add(new CriteriaItem(sha1(random_int(PHP_INT_MIN, PHP_INT_MAX))), $conditionVar);
39+
$compo2 = new CriteriaCompo(new CriteriaItem(sha1((string) random_int(PHP_INT_MIN, PHP_INT_MAX))));
40+
$compo2->add(new CriteriaItem(sha1((string) random_int(PHP_INT_MIN, PHP_INT_MAX))), $conditionVar);
4241
$label = sprintf("One column with null value and condition %s", $conditionData->name);
4342
$testsVariations[$label] = [$compo2, $conditionVar];
4443

@@ -146,7 +145,7 @@ final public function testGroupBy(): void
146145
{
147146
$criteria = new CriteriaCompo();
148147
self::assertEmpty($criteria->getGroupBy(), 'Default group by is not empty');
149-
$groupBy = sha1(random_int(PHP_INT_MIN, PHP_INT_MAX));
148+
$groupBy = sha1((string) random_int(PHP_INT_MIN, PHP_INT_MAX));
150149
$criteria->setGroupBy($groupBy);
151150
self::assertNotEmpty($criteria->getGroupBy(), 'Group by was set but value wasn\'t modified');
152151
self::assertStringStartsWith('GROUP BY', trim($criteria->getGroupBy()), 'Non empty group by doesn\' starts with "GROUP BY"');
@@ -162,7 +161,7 @@ final public function testSortBy(): void
162161
{
163162
$criteria = new CriteriaCompo();
164163
self::assertEmpty($criteria->getSort(), 'Default sort by is not empty');
165-
$sort = sha1(random_int(PHP_INT_MIN, PHP_INT_MAX));
164+
$sort = sha1((string) random_int(PHP_INT_MIN, PHP_INT_MAX));
166165
$criteria->setSort($sort);
167166
self::assertNotEmpty($criteria->getSort(), 'Sort by was set but value wasn\'t modified');
168167
self::assertStringContainsString($sort, $criteria->getSort(), 'Sort by value doesn\'t exists');

tests/CriteriaItemTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CriteriaItemTest extends TestCase
2424
*/
2525
final public static function provideComparisonOperators(): array
2626
{
27-
$column = sha1(random_int(PHP_INT_MIN, PHP_INT_MAX));
27+
$column = sha1((string) random_int(PHP_INT_MIN, PHP_INT_MAX));
2828
$specialOperators = [
2929
ComparisonOperator::BETWEEN->name,
3030
ComparisonOperator::NOT_BETWEEN->name,
@@ -33,7 +33,7 @@ final public static function provideComparisonOperators(): array
3333
];
3434
$possibleValues = [
3535
null, // null value
36-
md5(random_int(PHP_INT_MIN, PHP_INT_MAX)), // random string
36+
md5((string) random_int(PHP_INT_MIN, PHP_INT_MAX)), // random string
3737
random_int(PHP_INT_MIN, PHP_INT_MAX), // random int
3838
mt_rand() / mt_getrandmax(), // random float
3939
true, // true
@@ -142,7 +142,7 @@ final public static function provideOrder(): Generator
142142
#[DataProvider('provideOrder')]
143143
final public function testOrder(Order|string $order): void
144144
{
145-
$criteria = new CriteriaItem(sha1(random_int(PHP_INT_MIN, PHP_INT_MAX)));
145+
$criteria = new CriteriaItem(sha1((string) random_int(PHP_INT_MIN, PHP_INT_MAX)));
146146
self::assertSame(Order::ASC->value, $criteria->getOrder()->value, 'Default order is not correct');
147147
$criteria->setOrder($order);
148148
self::assertSame(strtoupper(trim($order)), $criteria->getOrder()->value, 'Order ' . $order . ' does\'t sets');
@@ -155,9 +155,9 @@ final public function testOrder(Order|string $order): void
155155
*/
156156
final public function testGroupBy(): void
157157
{
158-
$criteria = new CriteriaItem(sha1(random_int(PHP_INT_MIN, PHP_INT_MAX)));
158+
$criteria = new CriteriaItem(sha1((string) random_int(PHP_INT_MIN, PHP_INT_MAX)));
159159
self::assertEmpty($criteria->getGroupBy(), 'Default group by is not empty');
160-
$groupBy = sha1(random_int(PHP_INT_MIN, PHP_INT_MAX));
160+
$groupBy = sha1((string) random_int(PHP_INT_MIN, PHP_INT_MAX));
161161
$criteria->setGroupBy($groupBy);
162162
self::assertNotEmpty($criteria->getGroupBy(), 'Group by was set but value wasn\'t modified');
163163
self::assertStringStartsWith('GROUP BY', trim($criteria->getGroupBy()), 'Non empty group by doesn\' starts with "GROUP BY"');
@@ -170,9 +170,9 @@ final public function testGroupBy(): void
170170
*/
171171
final public function testSortBy(): void
172172
{
173-
$criteria = new CriteriaItem(sha1(random_int(PHP_INT_MIN, PHP_INT_MAX)));
173+
$criteria = new CriteriaItem(sha1((string) random_int(PHP_INT_MIN, PHP_INT_MAX)));
174174
self::assertEmpty($criteria->getSort(), 'Default sort by is not empty');
175-
$sort = sha1(random_int(PHP_INT_MIN, PHP_INT_MAX));
175+
$sort = sha1((string) random_int(PHP_INT_MIN, PHP_INT_MAX));
176176
$criteria->setSort($sort);
177177
self::assertNotEmpty($criteria->getSort(), 'Sort by was set but value wasn\'t modified');
178178
self::assertStringContainsString($sort, $criteria->getSort(), 'Sort by value doesn\'t exists');
@@ -184,7 +184,7 @@ final public function testSortBy(): void
184184
*/
185185
final public function testPartialResults(): void
186186
{
187-
$criteria = new CriteriaItem(sha1(random_int(PHP_INT_MIN, PHP_INT_MAX)));
187+
$criteria = new CriteriaItem(sha1((string) random_int(PHP_INT_MIN, PHP_INT_MAX)));
188188
self::assertSame(0, $criteria->getLimit(), 'Default limit is not 0');
189189
self::assertSame(0, $criteria->getStart(), 'Default start is not 0');
190190
$limit = random_int(1, PHP_INT_MAX);

0 commit comments

Comments
 (0)