Skip to content

Commit 06423ed

Browse files
authored
Merge pull request #45 from bancer/phpstan
Add phpstan tests and fix all reported errors
2 parents 5fa005d + 3b68216 commit 06423ed

File tree

22 files changed

+473
-114
lines changed

22 files changed

+473
-114
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: 'Setup CakePHP'
2+
description: 'Clone CakePHP and setup it'
3+
4+
runs:
5+
using: "composite"
6+
steps:
7+
8+
- name: Clone CakePHP
9+
uses: actions/checkout@v3
10+
with:
11+
repository: cakephp/cakephp
12+
path: ./cakephp
13+
ref: 2.10.14
14+
15+
- name: Make tmp folder writable
16+
run: chmod -R 777 ./cakephp/app/tmp
17+
shell: bash
18+
19+
- name: Fix composer autoload
20+
run: |
21+
echo "<?php " > /tmp/core.php
22+
echo "require ROOT . '/vendors/autoload.php'; " >> /tmp/core.php
23+
echo "spl_autoload_unregister(array('App', 'load')); " >> /tmp/core.php
24+
echo "spl_autoload_register(array('App', 'load'), true, true); " >> /tmp/core.php
25+
echo "?>" >> /tmp/core.php
26+
cat ./cakephp/app/Config/core.php >> /tmp/core.php
27+
cp /tmp/core.php ./cakephp/app/Config/core.php
28+
shell: bash
29+
30+
- name: Copy plugin files to plugins folder
31+
run: |
32+
mkdir -p ./cakephp/plugins/Filter
33+
cp -R ./Controller ./cakephp/plugins/Filter/Controller
34+
cp -R ./Model ./cakephp/plugins/Filter/Model
35+
cp -R ./Test ./cakephp/plugins/Filter/Test
36+
cp -R ./View ./cakephp/plugins/Filter/View
37+
shell: bash
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: 'Setup Database'
2+
description: 'Create test database and generate database configuration file'
3+
4+
runs:
5+
using: "composite"
6+
steps:
7+
8+
- name: Create test database
9+
run: mysql -u root -e "CREATE DATABASE cakephp_test"
10+
shell: bash
11+
12+
- name: Generate database configuration file
13+
run: echo "<?php
14+
class DATABASE_CONFIG {
15+
private \$identities = array(
16+
'mysql' => array(
17+
'datasource' => 'Database/Mysql',
18+
'host' => '127.0.0.1',
19+
'login' => 'root'
20+
)
21+
);
22+
public \$default = array(
23+
'persistent' => false,
24+
'host' => '',
25+
'login' => '',
26+
'password' => '',
27+
'database' => 'cakephp_test',
28+
'prefix' => ''
29+
);
30+
public \$test = array(
31+
'persistent' => false,
32+
'host' => '',
33+
'login' => '',
34+
'password' => '',
35+
'database' => 'cakephp_test',
36+
'prefix' => ''
37+
);
38+
public function __construct() {
39+
\$db = 'mysql';
40+
if (!empty(\$_SERVER['DB'])) {
41+
\$db = \$_SERVER['DB'];
42+
}
43+
foreach (array('default', 'test') as \$source) {
44+
\$config = array_merge(\$this->{\$source}, \$this->identities[\$db]);
45+
if (is_array(\$config['database'])) {
46+
\$config['database'] = \$config['database'][\$source];
47+
}
48+
if (!empty(\$config['schema']) && is_array(\$config['schema'])) {
49+
\$config['schema'] = \$config['schema'][\$source];
50+
}
51+
\$this->{\$source} = \$config;
52+
}
53+
}
54+
}" > ./cakephp/app/Config/database.php
55+
shell: bash

.github/workflows/pull-request.yml

Lines changed: 24 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -22,80 +22,15 @@ jobs:
2222
mysql-version: '5.6'
2323
- name: Check out repository code
2424
uses: actions/checkout@v3
25-
- name: Clone CakePHP
26-
uses: actions/checkout@v3
27-
with:
28-
repository: cakephp/cakephp
29-
path: ./cakephp
30-
ref: 2.10.14
25+
- name: Setup CakePHP
26+
uses: ./.github/actions/setup-cakephp
27+
- name: Setup database
28+
uses: ./.github/actions/setup-database
3129
- name: Install PHPUnit
3230
run: |
3331
cd ./cakephp
34-
chmod -R 777 app/tmp
3532
composer require 'phpunit/phpunit=5.7'
3633
cd ../
37-
- name: Fix composer autoload
38-
run: |
39-
echo "<?php " > /tmp/core.php
40-
echo "require ROOT . '/vendors/autoload.php'; " >> /tmp/core.php
41-
echo "spl_autoload_unregister(array('App', 'load')); " >> /tmp/core.php
42-
echo "spl_autoload_register(array('App', 'load'), true, true); " >> /tmp/core.php
43-
echo "?>" >> /tmp/core.php
44-
cat ./cakephp/app/Config/core.php >> /tmp/core.php
45-
cp /tmp/core.php ./cakephp/app/Config/core.php
46-
- name: Copy plugin files to plugins folder
47-
run: |
48-
mkdir -p ./cakephp/plugins/Filter
49-
cp -R ./Controller ./cakephp/plugins/Filter/Controller
50-
cp -R ./Model ./cakephp/plugins/Filter/Model
51-
cp -R ./Test ./cakephp/plugins/Filter/Test
52-
cp -R ./View ./cakephp/plugins/Filter/View
53-
54-
- name: Setup database
55-
run: |
56-
mysql -u root -e "CREATE DATABASE cakephp_test"
57-
echo "<?php
58-
class DATABASE_CONFIG {
59-
private \$identities = array(
60-
'mysql' => array(
61-
'datasource' => 'Database/Mysql',
62-
'host' => '127.0.0.1',
63-
'login' => 'root'
64-
)
65-
);
66-
public \$default = array(
67-
'persistent' => false,
68-
'host' => '',
69-
'login' => '',
70-
'password' => '',
71-
'database' => 'cakephp_test',
72-
'prefix' => ''
73-
);
74-
public \$test = array(
75-
'persistent' => false,
76-
'host' => '',
77-
'login' => '',
78-
'password' => '',
79-
'database' => 'cakephp_test',
80-
'prefix' => ''
81-
);
82-
public function __construct() {
83-
\$db = 'mysql';
84-
if (!empty(\$_SERVER['DB'])) {
85-
\$db = \$_SERVER['DB'];
86-
}
87-
foreach (array('default', 'test') as \$source) {
88-
\$config = array_merge(\$this->{\$source}, \$this->identities[\$db]);
89-
if (is_array(\$config['database'])) {
90-
\$config['database'] = \$config['database'][\$source];
91-
}
92-
if (!empty(\$config['schema']) && is_array(\$config['schema'])) {
93-
\$config['schema'] = \$config['schema'][\$source];
94-
}
95-
\$this->{\$source} = \$config;
96-
}
97-
}
98-
}" > ./cakephp/app/Config/database.php
9934
10035
- name: Unit Tests
10136
run: ./cakephp/lib/Cake/Console/cake test Filter All --stderr -app ./cakephp/app
@@ -113,3 +48,23 @@ jobs:
11348
run: composer require --dev overtrue/phplint
11449
- name: Run PHPLint
11550
run: vendor/bin/phplint
51+
52+
PHPStan:
53+
runs-on: ubuntu-latest
54+
steps:
55+
- name: Setup PHP
56+
uses: shivammathur/setup-php@v2
57+
with:
58+
php-version: 7.4
59+
- name: Check out repository code
60+
uses: actions/checkout@v3
61+
- name: Setup CakePHP
62+
uses: ./.github/actions/setup-cakephp
63+
- name: Install PHPUnit
64+
run: composer require 'phpunit/phpunit=7.0'
65+
- name: Install PHPStan
66+
run: |
67+
composer require --dev phpstan/phpstan
68+
composer require --dev phpstan/phpstan-phpunit
69+
- name: PHPStan
70+
run: vendor/bin/phpstan analyse --level=8 ./cakephp/plugins/Filter

Controller/Component/FilterComponent.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,35 @@
2020
*/
2121
class FilterComponent extends Component
2222
{
23+
/**
24+
* @var string[]
25+
*/
2326
public $components = array('Session');
2427

28+
/**
29+
* @var mixed[]
30+
*/
2531
public $settings = array();
32+
33+
/**
34+
* @var mixed[]
35+
*/
2636
public $nopersist = array();
37+
38+
/**
39+
* @var mixed[]
40+
*/
2741
public $formData = array();
42+
43+
/**
44+
* @var mixed[]
45+
*/
2846
protected $_request_settings = array();
2947

48+
/**
49+
* @param \ComponentCollection $collection
50+
* @param mixed[] $settings
51+
*/
3052
public function __construct(ComponentCollection $collection, $settings = array())
3153
{
3254
parent::__construct($collection, $settings);
@@ -199,7 +221,11 @@ public function beforeRender(Controller $controller)
199221
$options['type'] = 'select';
200222

201223
$selectOptions = array();
224+
/** @var \Model|bool $workingModel */
202225
$workingModel = ClassRegistry::init($fieldModel);
226+
if (is_bool($workingModel)) {
227+
throw new MissingModelException(array($fieldModel));
228+
}
203229

204230
if (isset($settings['selectOptions']))
205231
{
@@ -318,6 +344,11 @@ public function beforeRender(Controller $controller)
318344
$controller->set('viewFilterParams', $viewFilterParams);
319345
}
320346

347+
/**
348+
* @param \Controller $controller
349+
* @param mixed[] $settings
350+
* @return void
351+
*/
321352
private function __updatePersistence($controller, $settings)
322353
{
323354
if ($this->Session->check('FilterPlugin.NoPersist'))

0 commit comments

Comments
 (0)