Skip to content

Commit c7013b2

Browse files
authored
Merge pull request #31 from MekDrop/add-phpcs
Added phpcs
2 parents dd845fd + 23c4add commit c7013b2

17 files changed

+169
-105
lines changed

.github/workflows/on_pull_request.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ jobs:
3434
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader
3535
- name: PHPStan
3636
run: composer phpstan
37+
- name: PHPCS
38+
run: composer phpcs
3739
- name: PHPUnit
3840
run: composer test
3941
- name: Install Composer dependencies (without dev)

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929
],
3030
"scripts": {
3131
"test": "phpunit --testdox",
32-
"phpstan": "phpstan analyse"
32+
"phpstan": "phpstan analyse",
33+
"phpcs": "vendor/bin/phpcs",
34+
"phpcbf": "vendor/bin/phpcbf"
3335
},
3436
"require-dev": {
3537
"phpunit/phpunit": "^12",
36-
"phpstan/phpstan": "^2.1"
38+
"phpstan/phpstan": "^2.1",
39+
"squizlabs/php_codesniffer": "^3.13",
40+
"fakerphp/faker": "^1.24"
3741
}
3842
}

phpcs.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Project Rules">
3+
<description>Project coding standard using PSR-12.</description>
4+
<rule ref="PSR12"/>
5+
<file>src</file>
6+
<file>tests</file>
7+
</ruleset>
8+

src/CriteriaCompo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,4 @@ public function __toString(): string
117117
{
118118
return $this->render() . 'A';
119119
}
120-
}
120+
}

src/CriteriaElement.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
*/
1919
abstract class CriteriaElement implements Stringable
2020
{
21-
use GroupByTrait, OrderByTrait, PartialResultsTrait, RenderingTrait, SortByTrait;
21+
use GroupByTrait;
22+
use OrderByTrait;
23+
use PartialResultsTrait;
24+
use RenderingTrait;
25+
use SortByTrait;
2226

2327
/**
2428
* Gets data for rendered query for binding
@@ -85,4 +89,4 @@ public function __isset(string $name): bool
8589
default => false,
8690
};
8791
}
88-
}
92+
}

src/CriteriaItem.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
class CriteriaItem extends CriteriaElement
1818
{
19-
2019
protected ?string $prefix = null;
2120
protected ?string $function = null;
2221
protected null|string|int $column = null;
@@ -39,19 +38,18 @@ class CriteriaItem extends CriteriaElement
3938
* @param string|null $function
4039
*/
4140
public function __construct(
42-
string|int $column,
43-
mixed $value = null,
41+
string|int $column,
42+
mixed $value = null,
4443
ComparisonOperator|string $operator = '=',
45-
?string $prefix = null,
46-
?string $function = null
47-
)
48-
{
44+
?string $prefix = null,
45+
?string $function = null
46+
) {
4947
parent::__construct();
5048

5149
$this->prefix = $prefix;
5250
$this->function = $function;
5351
$this->column = $column;
54-
$this->operator = $operator instanceof ComparisonOperator ? $operator : ComparisonOperator::from(strtoupper(trim($operator)));
52+
$this->operator = ComparisonOperator::resolve($operator);
5553

5654
if (is_string($value) && str_starts_with($value, '(')) {
5755
$this->data[] = $value;
@@ -140,9 +138,17 @@ public function render(bool $withBindVariables = false): ?string
140138
break;
141139
default:
142140
if ($withBindVariables) {
143-
$clause .= sprintf(" %s %s", $this->operator->value, $this->prepareRenderedValue(current($this->data)));
141+
$clause .= sprintf(
142+
" %s %s",
143+
$this->operator->value,
144+
$this->prepareRenderedValue(current($this->data))
145+
);
144146
} else {
145-
$clause .= sprintf(" %s :%s", $this->operator->value, key($this->data));
147+
$clause .= sprintf(
148+
" %s :%s",
149+
$this->operator->value,
150+
key($this->data)
151+
);
146152
}
147153
}
148154

@@ -172,7 +178,7 @@ protected function prepareRenderedValue(null|bool|object|string|float|array $val
172178
}
173179

174180
if (is_object($value)) {
175-
if ((interface_exists(Stringable::class) && ($value instanceof Stringable)) || method_exists($value, '__toString')) {
181+
if ($value instanceof Stringable) {
176182
return $this->addSlashes((string)$value);
177183
}
178184

@@ -234,4 +240,4 @@ public function __toString(): string
234240
{
235241
return $this->render();
236242
}
237-
}
243+
}

src/Enum/ComparisonOperator.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
enum ComparisonOperator: string
1111
{
12-
1312
/**
1413
* NULL value test
1514
*/
@@ -99,4 +98,13 @@ enum ComparisonOperator: string
9998
* Whether string matches regular expression
10099
*/
101100
case RLIKE = 'RLIKE';
102-
}
101+
102+
public static function resolve(string|ComparisonOperator $operator): ComparisonOperator
103+
{
104+
if (is_string($operator)) {
105+
return self::from(strtoupper(trim($operator)));
106+
}
107+
108+
return $operator;
109+
}
110+
}

src/Enum/Condition.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
enum Condition: string
1111
{
12-
1312
/**
1413
* Use AND
1514
*/
@@ -24,5 +23,4 @@ enum Condition: string
2423
* Use XOR
2524
*/
2625
case XOR = 'XOR';
27-
28-
}
26+
}

src/Enum/Order.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
namespace Imponeer\Database\Criteria\Enum;
44

5-
65
/**
76
* Defines how to order results
87
*
98
* @package Imponeer\Database\Criteria\Enum
109
*/
1110
enum Order: string
1211
{
13-
1412
/**
1513
* Sort in ASC mode
1614
*/
@@ -20,5 +18,4 @@ enum Order: string
2018
* Sort in DESC mode
2119
*/
2220
case DESC = 'DESC';
23-
24-
}
21+
}

src/Helpers/UniqueBindParam.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class UniqueBindParam
1212
/**
1313
* Last generated bind suffix ID
1414
*/
15-
static private int $lastGeneratedBindSuffixId = 0;
15+
private static int $lastGeneratedBindSuffixId = 0;
1616

1717
/**
1818
* UniqueBindParam constructor.
@@ -30,5 +30,4 @@ public static function generate(string $column): string
3030
{
3131
return 'col_' . $column . '_' . (++self::$lastGeneratedBindSuffixId);
3232
}
33-
34-
}
33+
}

0 commit comments

Comments
 (0)