Skip to content

Commit 4d27c5d

Browse files
committed
mass refactor
1 parent ce9b8db commit 4d27c5d

14 files changed

+2001
-84
lines changed

.github/workflows/test_master.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
test:
13+
name: Test
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
php: ['7.4', '8.0', '8.1']
18+
19+
steps:
20+
- name: Set up PHP
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: ${{ matrix.php }}
24+
coverage: xdebug
25+
tools: composer:v2
26+
27+
- name: Checkout code
28+
uses: actions/checkout@v2
29+
with:
30+
fetch-depth: 0
31+
32+
- name: PHP Version Check
33+
run: php -v
34+
35+
- name: Validate Composer JSON
36+
run: composer validate
37+
38+
- name: Run Composer
39+
run: composer install --no-interaction
40+
41+
- name: Unit tests
42+
run: |
43+
composer test-init
44+
composer test
45+
46+
- name: PHP Code Sniffer
47+
run: composer codesniffer
48+
49+
- name: PHPStan analysis
50+
run: composer stan
51+
52+
# code-coverage:
53+
# name: Code coverage
54+
# runs-on: ubuntu-latest
55+
# strategy:
56+
# matrix:
57+
# php: ['7.4']
58+
#
59+
# steps:
60+
# - name: Set up PHP
61+
# uses: shivammathur/setup-php@v2
62+
# with:
63+
# php-version: ${{ matrix.php }}
64+
# coverage: xdebug
65+
# tools: composer:v2
66+
#
67+
# - name: Checkout code
68+
# uses: actions/checkout@v2
69+
# with:
70+
# fetch-depth: 0
71+
#
72+
# - name: Run Composer
73+
# run: composer install --no-interaction
74+
#
75+
# - name: Unit tests
76+
# run: |
77+
# composer test-init
78+
# composer test-coverage-xml
79+
# mkdir -p ./build/logs
80+
# cp ./tests/_output/coverage.xml ./build/logs/clover.xml
81+
# - name: Code Coverage (Coveralls)
82+
# env:
83+
# COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
84+
# run: php vendor/bin/php-coveralls -v

codeception.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
actor: Tester
2+
bootstrap: _bootstrap.php
3+
paths:
4+
tests: tests
5+
log: tests/_output
6+
output: tests/_output
7+
data: tests/_data
8+
helpers: tests/_support
9+
settings:
10+
memory_limit: 1024M
11+
colors: true
12+
coverage:
13+
enabled: true
14+
show_uncovered: false
15+
include:
16+
- src/*
17+
exclude:
18+
- vendor/*
19+
- tests/*

composer.json

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@
1111
}
1212
],
1313
"require": {
14-
"php": ">=7.2.0",
14+
"php": ">=7.4",
1515
"ext-pdo": "*",
16-
"yiisoft/yii2": "~2.0.0",
17-
"smoren/query-relation-manager": "2.0.4"
16+
"yiisoft/yii2": "^2.0.0",
17+
"smoren/query-relation-manager": "dev-master"
18+
},
19+
"require-dev": {
20+
"codeception/codeception": "^4.2.1",
21+
"codeception/module-asserts": "^2.0",
22+
"php-coveralls/php-coveralls": "^2.0",
23+
"squizlabs/php_codesniffer": "3.*",
24+
"phpstan/phpstan": "^1.8"
1825
},
1926
"autoload": {
2027
"psr-4": {
21-
"Smoren\\Yii2\\QueryRelationManager\\": "src"
28+
"Smoren\\QueryRelationManager\\Yii2\\": "src",
29+
"Smoren\\QueryRelationManager\\Yii2\\Tests\\Unit\\": "tests/unit"
2230
}
2331
},
2432
"config": {
@@ -31,5 +39,15 @@
3139
"type": "composer",
3240
"url": "https://asset-packagist.org"
3341
}
34-
]
42+
],
43+
"scripts": {
44+
"test-init": ["./vendor/bin/codecept build"],
45+
"test-all": ["composer test-coverage", "composer codesniffer", "composer stan"],
46+
"test": ["./vendor/bin/codecept run unit tests/unit"],
47+
"test-coverage": ["./vendor/bin/codecept run unit tests/unit --coverage"],
48+
"test-coverage-html": ["./vendor/bin/codecept run unit tests/unit --coverage-html"],
49+
"test-coverage-xml": ["./vendor/bin/codecept run unit tests/unit --coverage-xml"],
50+
"codesniffer": ["./vendor/bin/phpcs --ignore=vendor,tests --standard=tests/coding_standard.xml -s ."],
51+
"stan": ["./vendor/bin/phpstan analyse -l 9 src"]
52+
}
3553
}
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<?php
22

3+
namespace Smoren\QueryRelationManager\Yii2;
34

4-
namespace Smoren\Yii2\QueryRelationManager\Yii2;
5-
6-
7-
use Smoren\Yii2\QueryRelationManager\Base\QueryRelationManagerException;
5+
use Smoren\QueryRelationManager\Base\QueryRelationManagerException;
86

97
/**
108
* Trait для упрощения построения запросов с помощью QueryRelationManager
11-
* @package Smoren\Yii2\QueryRelationManager\ActiveRecord
129
* @author Smoren <ofigate@gmail.com>
10+
* @method static string tableName()
1311
*/
1412
trait ActiveRecordTrait
1513
{
@@ -21,8 +19,6 @@ trait ActiveRecordTrait
2119
*/
2220
public static function select(?string $alias = null): QueryRelationManager
2321
{
24-
return QueryRelationManager::select(
25-
self::class, $alias ?? self::tableName()
26-
);
22+
return QueryRelationManager::select(self::class, $alias ?? self::tableName());
2723
}
28-
}
24+
}

src/Yii2/QueryRelationDataProvider.php renamed to src/QueryRelationDataProvider.php

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
<?php
22

3+
namespace Smoren\QueryRelationManager\Yii2;
34

4-
namespace Smoren\Yii2\QueryRelationManager\Yii2;
5-
6-
7-
use Smoren\Yii2\QueryRelationManager\Base\QueryRelationManagerException;
5+
use Smoren\QueryRelationManager\Base\QueryRelationManagerException;
86
use yii\data\BaseDataProvider;
97
use yii\db\Connection;
108
use yii\db\Query;
119

1210
/**
1311
* DataProvider для организации постраничной навигации в QueryRelationManager
14-
* @package Smoren\Yii2\QueryRelationManager\ActiveRecord
1512
* @author Smoren <ofigate@gmail.com>
1613
*/
1714
class QueryRelationDataProvider extends BaseDataProvider
1815
{
1916
/**
2017
* @var QueryRelationManager объект QueryRelationManager
2118
*/
22-
public $queryRelationManager;
19+
public QueryRelationManager $queryRelationManager;
2320

2421
/**
25-
* @var Connection|array|string the DB connection object or the application component ID of the DB connection.
22+
* @var Connection|null the DB connection object or the application component ID of the DB connection.
2623
* If not set, the default DB connection will be used.
27-
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
2824
*/
29-
public $db;
25+
public ?Connection $db;
3026

3127
/**
3228
* @var string|callable имя столбца с ключом или callback-функция, возвращающие его
@@ -36,30 +32,23 @@ class QueryRelationDataProvider extends BaseDataProvider
3632
/**
3733
* @var bool Не считать totalCount
3834
*/
39-
public $withoutTotalCount = false;
35+
public bool $withoutTotalCount = false;
4036

4137
/**
4238
* @inheritDoc
4339
*/
44-
public function init()
40+
public function init(): void
4541
{
4642
parent::init();
47-
48-
if(!($this->queryRelationManager instanceof QueryRelationManager)) {
49-
throw new QueryRelationManagerException(
50-
"param QueryRelationDataProvider::queryRelationManager is not an instance of QueryRelationManager"
51-
);
52-
}
53-
5443
$this->queryRelationManager = clone $this->queryRelationManager;
5544
}
5645

5746
/**
5847
* Prepares the data models that will be made available in the current page.
59-
* @return array the available data models
48+
* @return array<mixed> the available data models
6049
* @throws QueryRelationManagerException
6150
*/
62-
protected function prepareModels()
51+
protected function prepareModels(): array
6352
{
6453
$pagination = $this->getPagination();
6554

@@ -106,9 +95,11 @@ protected function prepareModels()
10695
$pkValuesPrefixed[] = $rowPrefixed;
10796
}
10897

109-
$models = $this->queryRelationManager->filter(function(Query $q) use ($pkFields, $pkValuesPrefixed, $mainTable) {
110-
$q->andWhere(['in', $pkFields, $pkValuesPrefixed]);
111-
})->all();
98+
$models = $this->queryRelationManager->filter(
99+
function(Query $q) use ($pkFields, $pkValuesPrefixed) {
100+
$q->andWhere(['in', $pkFields, $pkValuesPrefixed]);
101+
}
102+
)->all();
112103
}
113104
}
114105

@@ -117,12 +108,13 @@ protected function prepareModels()
117108

118109
/**
119110
* Prepares the keys associated with the currently available data models.
120-
* @param array $models the available data models
121-
* @return array the keys
111+
* @param array<array<string, mixed>> $models the available data models
112+
* @return array<scalar> the keys
122113
*/
123-
protected function prepareKeys($models)
114+
protected function prepareKeys($models): array
124115
{
125116
if($this->key !== null) {
117+
/** @var array<scalar> $keys */
126118
$keys = [];
127119

128120
foreach($models as $model) {
@@ -144,17 +136,17 @@ protected function prepareKeys($models)
144136
* @return int total number of data models in this data provider.
145137
* @throws QueryRelationManagerException
146138
*/
147-
protected function prepareTotalCount()
139+
protected function prepareTotalCount(): int
148140
{
149141
if($this->withoutTotalCount) {
150142
return 0;
151143
}
152144

153-
return $this->queryRelationManager
145+
return (int)$this->queryRelationManager
154146
->prepare()
155147
->getQuery()
156148
->select($this->queryRelationManager->getTableCollection()->getMainTable()->getPrimaryKeyForSelect())
157149
->distinct()
158150
->count();
159151
}
160-
}
152+
}

0 commit comments

Comments
 (0)