Skip to content

Commit

Permalink
Add PHPStan to CI (#1549)
Browse files Browse the repository at this point in the history
* Add PHPStan to CI

* fix memory limit

* level = 0

* do not rely solely on phpdoc

* exclude new static rule

* fix undefined var

* fix missing use

* fix method signature

* phpstan can not analyse included variables

* level = 1

* fix some issues

* fix phpstan config escape

* fix return mixed type with null

* skip ignore for level=2

* fix model->expr()

* add generated ignores

* increase level to 2

* fix unneeded ignore

* fix wrong phpdoc

* fix duplicate keys in ScopeBuilder

* unify value of OPERATOR_TEXT_EQUALS
  • Loading branch information
mvorisek authored Dec 2, 2020
1 parent 865a201 commit 34a6da6
Show file tree
Hide file tree
Showing 26 changed files with 381 additions and 41 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
include:
- php: 'latest'
type: 'CodingStyle'
- php: 'latest'
type: 'StaticAnalysis'
env:
LOG_COVERAGE: ""
steps:
Expand All @@ -46,9 +48,9 @@ jobs:
- name: Install PHP dependencies
run: |
if [ "${{ matrix.type }}" != "Phpunit" ]; then composer remove --no-interaction --no-update phpunit/phpunit johnkary/phpunit-speedtrap phpunit/phpcov --dev ; fi
if [ "${{ matrix.type }}" != "Phpunit" ] && [ "${{ matrix.type }}" != "StaticAnalysis" ]; then composer remove --no-interaction --no-update phpunit/phpunit johnkary/phpunit-speedtrap phpunit/phpcov --dev ; fi
if [ "${{ matrix.type }}" != "CodingStyle" ]; then composer remove --no-interaction --no-update friendsofphp/php-cs-fixer --dev ; fi
composer remove --no-interaction --no-update 'behat/*' --dev
if [ "${{ matrix.type }}" != "StaticAnalysis" ]; then composer remove --no-interaction --no-update phpstan/phpstan 'behat/*' --dev ; fi
if [ "${{ matrix.php }}" == "8.0" ]; then composer config platform.php 7.4.5 ; fi
composer install --no-suggest --ansi --prefer-dist --no-interaction --no-progress --optimize-autoloader
Expand Down Expand Up @@ -79,6 +81,12 @@ jobs:
if [ "$(find demos/ -name '*.php' -print0 | xargs -0 grep -L "namespace atk4\\\\ui\\\\demo;" | tee /dev/fd/2)" ]; then echo 'All demos/ files must have namespace declared' && (exit 1); fi
vendor/bin/php-cs-fixer fix --dry-run --using-cache=no --diff --diff-format=udiff --verbose --show-progress=dots
- name: Run Static Analysis (only for StaticAnalysis)
if: matrix.type == 'StaticAnalysis'
run: |
echo "memory_limit = 1G" > /usr/local/etc/php/conf.d/custom-memory-limit.ini
vendor/bin/phpstan analyse
unit-test:
name: Unit
runs-on: ubuntu-latest
Expand Down Expand Up @@ -132,7 +140,7 @@ jobs:
run: |
if [ "${{ matrix.type }}" != "Phpunit" ] && [ "${{ matrix.type }}" != "Phpunit Lowest" ] && [ "${{ matrix.type }}" != "Phpunit Burn" ]; then composer remove --no-interaction --no-update phpunit/phpunit johnkary/phpunit-speedtrap phpunit/phpcov --dev ; fi
if [ "${{ matrix.type }}" != "CodingStyle" ]; then composer remove --no-interaction --no-update friendsofphp/php-cs-fixer --dev ; fi
composer remove --no-interaction --no-update 'behat/*' --dev
if [ "${{ matrix.type }}" != "StaticAnalysis" ]; then composer remove --no-interaction --no-update phpstan/phpstan 'behat/*' --dev ; fi
if [ "${{ matrix.php }}" == "8.0" ]; then composer config platform.php 7.4.5 ; fi
composer install --no-suggest --ansi --prefer-dist --no-interaction --no-progress --optimize-autoloader
if [ "${{ matrix.type }}" == "Phpunit Lowest" ]; then composer update --ansi --prefer-dist --prefer-lowest --prefer-stable --no-interaction --no-progress --optimize-autoloader ; fi
Expand Down Expand Up @@ -261,6 +269,7 @@ jobs:
run: |
composer remove --no-interaction --no-update phpunit/phpunit johnkary/phpunit-speedtrap phpunit/phpcov --dev
composer remove --no-interaction --no-update friendsofphp/php-cs-fixer --dev
composer remove --no-interaction --no-update phpstan/phpstan --dev
composer install --no-suggest --ansi --prefer-dist --no-interaction --no-progress --optimize-autoloader
if [ "${{ matrix.type }}" == "Chrome Lowest" ]; then composer update --ansi --prefer-dist --prefer-lowest --prefer-stable --no-interaction --no-progress --optimize-autoloader ; fi
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"instaclick/php-webdriver": "^1.4.7",
"guzzlehttp/guzzle": "^6.3",
"johnkary/phpunit-speedtrap": "^3.2",
"phpstan/phpstan": "^0.12.58",
"phpunit/phpcov": "*",
"phpunit/phpunit": ">=9.3",
"symfony/process": "^4.4 || ^5.0"
Expand Down
10 changes: 4 additions & 6 deletions demos/_includes/Persistence_Faker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@

class Persistence_Faker extends \atk4\data\Persistence
{
/** @var \Faker\Generator */
public $faker;

/** @var int */
public $count = 5;

public function __construct($opts = [])
public function __construct()
{
//parent::__construct($opts);

if (!$this->faker) {
$this->faker = \Faker\Factory::create();
}
$this->faker = \Faker\Factory::create();
}

public function prepareIterator($model)
Expand Down
4 changes: 2 additions & 2 deletions demos/_unit-test/exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
$modal = \atk4\ui\Modal::addTo($app, ['cb' => $cb]);
$modal->name = 'm_test';

$modal->set(function ($m) use ($modal) {
$modal->set(function ($m) {
throw new \Exception('TEST!');
});

Expand All @@ -27,7 +27,7 @@
$cb1 = CallbackLater::addTo($app, ['urlTrigger' => 'm2_cb']);
$modal2 = \atk4\ui\Modal::addTo($app, ['cb' => $cb1]);

$modal2->set(function ($m) use ($modal2) {
$modal2->set(function ($m) {
trigger_error('error triggered');
});

Expand Down
2 changes: 1 addition & 1 deletion demos/_unit-test/lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
$edit = $model->getUserAction('edit');
$edit->ui = ['execButton' => [\atk4\ui\Button::class, 'EditMe', 'blue']];
$edit->description = 'edit';
$edit->callback = function ($model) use ($app) {
$edit->callback = function ($model) {
return $model->ref('product_category_id')->getTitle() . ' - ' . $model->ref('product_sub_category_id')->getTitle();
};

Expand Down
8 changes: 4 additions & 4 deletions demos/_unit-test/scope-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
});

$expectedWord = <<<'EOF'
Project Budget is greater or equal to '1000'
and (Project Name is regular expression '[a-zA-Z]'
and Client Country Iso is equal to 'Brazil' and Start Date is equal to '2020-10-22')
Project Budget is greater or equal to '1000'
and (Project Name is regular expression '[a-zA-Z]'
and Client Country Iso is equal to 'Brazil' and Start Date is equal to '2020-10-22')
and (Finish Time is not equal to '22:22' or Is Commercial is equal to '0' or Currency is equal to 'USD')
EOF;

Expand Down Expand Up @@ -124,7 +124,7 @@
"type": "query-builder-rule",
"query": {
"rule": "is_commercial",
"operator": "is exactly",
"operator": "equals",
"value": "0",
"option": null
}
Expand Down
2 changes: 1 addition & 1 deletion demos/form/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
$form->addControl('control', [Form\Control\Calendar::class, 'type' => 'date', 'caption' => 'Date using form control: ']);
$form->buttonSave->set('Compare Date');

$form->onSubmit(function (Form $form) use ($app) {
$form->onSubmit(function (Form $form) {
$message = 'field = ' . print_r($form->model->get('field'), true) . '; <br> control = ' . print_r($form->model->get('control'), true);
$view = new \atk4\ui\Message('Date field vs control:');
$view->invokeInit();
Expand Down
6 changes: 3 additions & 3 deletions demos/form/jscondform.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
$formSubscribe->addControl('f_gift', [Form\Control\Dropdown::class, 'caption' => 'Gift for Women', 'values' => ['Wine Glass', 'Lipstick']]);

// Show email and gender when subscribe is checked.
// Show m_gift when gender is exactly equal to 'male' and subscribe is checked.
// Show f_gift when gender is exactly equal to 'female' and subscribe is checked.
// Show m_gift when gender = 'male' and subscribe is checked.
// Show f_gift when gender = 'female' and subscribe is checked.
$formSubscribe->setControlsDisplayRules([
'email' => ['subscribe' => 'checked'],
'gender' => ['subscribe' => 'checked'],
Expand All @@ -54,7 +54,7 @@
\atk4\ui\Header::addTo($app, ['Dog registration', 'size' => 2]);

$formDog = Form::addTo($app, ['segment']);
\atk4\ui\Label::addTo($formDog, ['You can select type of hair cut only with race that contains "poodle" AND age no more than 5 year OR your dog race is exactly "bichon".', 'top attached'], ['AboveControls']);
\atk4\ui\Label::addTo($formDog, ['You can select type of hair cut only with race that contains "poodle" AND age no more than 5 year OR your dog race equals "bichon".', 'top attached'], ['AboveControls']);
$formDog->addControl('race', [Form\Control\Line::class]);
$formDog->addControl('age');
$formDog->addControl('hair_cut', [Form\Control\Dropdown::class, 'values' => ['Short', 'Long']]);
Expand Down
1 change: 1 addition & 0 deletions demos/init-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
}

try {
/** @var \atk4\data\Persistence\Sql $db */
require_once __DIR__ . '/init-db.php';
$app->db = $db;
unset($db);
Expand Down
2 changes: 1 addition & 1 deletion demos/init-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected function init(): void
$this->addField('is_folder', ['type' => 'boolean']);

$this->hasMany('SubFolder', [new self(), 'their_field' => 'parent_folder_id'])
->addField('count', ['aggregate' => 'count', 'field' => $this->expr('*')]);
->addField('count', ['aggregate' => 'count', 'field' => $this->persistence->expr($this, '*')]);

$this->hasOne('parent_folder_id', Folder::class)
->addTitle();
Expand Down
4 changes: 2 additions & 2 deletions demos/interactive/accordion-nested.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}

// dynamic section - simple view
$i2 = $accordion->addSection('Dynamic Text', function ($v) use ($maxDepth, $level) {
$i2 = $accordion->addSection('Dynamic Text', function ($v) use ($addAccordionFunc, $maxDepth, $level) {
\atk4\ui\Message::addTo($v, ['Every time you open this accordion item, you will see a different text', 'ui' => 'tiny message']);
\atk4\ui\LoremIpsum::addTo($v, ['size' => 2]);
if ($level < $maxDepth) {
Expand All @@ -36,7 +36,7 @@
});

// dynamic section - form view
$i3 = $accordion->addSection('Dynamic Form', function ($v) use ($maxDepth, $level) {
$i3 = $accordion->addSection('Dynamic Form', function ($v) use ($addAccordionFunc, $maxDepth, $level) {
\atk4\ui\Message::addTo($v, ['Loading a form dynamically.', 'ui' => 'tiny message']);
$form = \atk4\ui\Form::addTo($v);
$form->addControl('Email');
Expand Down
2 changes: 1 addition & 1 deletion demos/interactive/popup.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function linkCart($cart, $jsAction = null)
$cartOutterLabel->addStyle('display', 'none');
}

$cartPopup->set(function ($popup) use ($shelf, $cartOutterLabel, $cart) {
$cartPopup->set(function ($popup) use ($cart) {
$cartInnerLabel = \atk4\ui\Label::addTo($popup, ['Number of items:']);

// cart is already initialized, so init() is not called again. However, cart will be rendered
Expand Down
4 changes: 2 additions & 2 deletions docs/form.rst
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,8 @@ Here is a more advanced example::

// Show email and gender when subscribe is checked.

// Show m_gift when gender is exactly equal to 'male' and subscribe is checked.
// Show f_gift when gender is exactly equal to 'female' and subscribe is checked.
// Show m_gift when gender = 'male' and subscribe is checked.
// Show f_gift when gender = 'female' and subscribe is checked.

$f_sub->setControlsDisplayRules([
'email' => ['subscribe' => 'checked'],
Expand Down
Loading

0 comments on commit 34a6da6

Please sign in to comment.