SlideShare a Scribd company logo
1 of 51
Download to read offline
Coding style,
Static code analysis and PHP
1
Outline
About me
What's Coding style?
PSR-2與PSR-12程式碼⾵格標準。
What's static code analysis?
PHPStan
Psalm
Phan
CI/CD examples
Laravel framework integration
2
About me
Peter
Active open source contributor
An associate engineer
DevOps
Back-end
System Architecture Researching
Web Application Security
PHP, Python and JavaScript
Smart Grid Technology (2017~2021)
Database, Data platform architecture (2021~)
GitHub
3
What's coding style?
AKA Programming style
4
PHP有Coding style嗎?
5
PHP有Coding style嗎?
Code Style Guide
6
Coding style
Founded by PHP-FIG
PHP Framework Interop Group
PSR-1
PSR-2
PSR-12
More standard docs
https://www.php-fig.org
https://github.com/php-fig
https://github.com/php-fig/fig-standards/tree/master/accepted
7
PSR-1 Overview
Files MUST use only <?php and <?= tags.
Files MUST use only UTF-8 without BOM for PHP code.
Files SHOULD either declare symbols (classes, functions, constants, etc.)
or cause side-effects (e.g. generate output, change .ini settings, etc.) but
SHOULD NOT do both.
Namespaces and classes MUST follow an "autoloading" PSR: [PSR-0, PSR-
4].
Class names MUST be declared in StudlyCaps.
Class constants MUST be declared in all upper case with underscore
separators.
Method names MUST be declared in camelCase.
 
8
PSR-2 Overview(Deprecated)
Code MUST follow a "coding style guide" PSR [ ].
Code MUST use 4 spaces for indenting, not tabs.
There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines SHOULD be 80 characters or less.
There MUST be one blank line after the namespace declaration, and there MUST be one blank line after the block of use
declarations.
Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body.
Opening braces for methods MUST go on the next line, and closing braces MUST go on the next line after the body.
Visibility MUST be declared on all properties and methods; abstract and final MUST be declared before the visibility; static
MUST be declared after the visibility.
Control structure keywords MUST have one space after them; method and function calls MUST NOT.
Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body.
Opening parentheses for control structures MUST NOT have a space after them, and closing parentheses for control
structures MUST NOT have a space before.
PSR-1
9
PSR-12
This specification extends, expands and replaces PSR-2, the
coding style guide and requires adherence to PSR-1, the basic
coding standard.
10
11
規則太多要檢查,有沒有檢查⼯具?
12
PHP_CodeSniffer
PHP-CS-Fixer
13
PHP_CodeSniffer
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
chmod +x phpcs.phar
mv phpcs.phar phpcs
phpcs --help
phpcs --standard=PSR2 src/ tests/
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
chmod +x phpcbf.phar
mv phpcbf.phar phpcbf
phpcbf --help
phpcbf --standard=PSR2 src/ tests/
14
phpcs --standard=PSR2
FILE: ...n-source-contributions/localized/src/Validation/LtValidation.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
31 | ERROR | [x] Use single instead of double quotes for simple
| | strings.
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
FILE: ...is/build/open-source-contributions/localized/tests/bootstrap.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
15 | ERROR | [x] Use single instead of double quotes for simple
| | strings.
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
FILE: ...n-source-contributions/localized/src/Validation/BrValidation.php
----------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
----------------------------------------------------------------------
196 | ERROR | [x] Use single instead of double quotes for simple
| | strings.
196 | ERROR | [x] Use single instead of double quotes for simple
| | strings.
----------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
15
phpcs --standard=PSR2
16
phpcbf --standard=PSR2
17
phpcs.xml
<?xml version="1.0"?>
<ruleset name="Coding Standard">
<arg name="basepath" value="."/>
<arg name="colors"/>
<arg value="sp"/>
<config name="ignore_warnings_on_exit" value="1"/>
<file>./src</file>
<file>./tests</file>
<rule ref="PSR2"></rule>
<!-- <rule ref="PSR12"></rule> -->
<rule ref="Squiz.Commenting.ClassComment">
<exclude name="Squiz.Commenting.ClassComment.TagNotAllowed"/>
<type>warning</type>
<exclude-pattern>*/tests/</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting.ClassComment.Missing">
<type>warning</type>
</rule>
<rule ref="Squiz.Commenting.FunctionComment.Missing">
<type>warning</type>
<exclude-pattern>*/config/</exclude-pattern>
/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
phpcs.xml.dist
18
PHP-CS-Fixer
curl -OL https://cs.symfony.com/download/php-cs-fixer-v2.phar
php php-cs-fixer-v2.phar fix --dry-run --format=txt --verbose --diff --diff-
format=udiff --config=.cs.php
curl -OL https://cs.symfony.com/download/php-cs-fixer-v3.phar
php php-cs-fixer-v3.phar fix --dry-run --format=txt --verbose --diff --diff-
format=udiff --config=.cs.php
19
.cs.php
<?php
return PhpCsFixerConfig::create()
->setUsingCache(false)
->setRiskyAllowed(true)
//->setCacheFile(__DIR__ . '/.php_cs.cache')
->setRules([
'@PSR1' => true,
'@PSR2' => true,
'@Symfony' => true,
'psr4' => true,
'yoda_style' => false,
'array_syntax' => ['syntax' => 'short'],
'list_syntax' => ['syntax' => 'short'],
'concat_space' => ['spacing' => 'one'],
'cast_spaces' => ['space' => 'none'],
'compact_nullable_typehint' => true,
'increment_style' => ['style' => 'post'],
'declare_equal_normalize' => ['space' => 'single'],
'no_short_echo_tag' => true,
'protected_to_private' => false,
'phpdoc_align' => false,
'phpdoc_add_missing_param_annotation' => ['only_untyped' => false],
'phpdoc_order' => true, // psr-5
'phpdoc_no_empty_return' => false,
'align multiline comment' => true, // psr-5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
PHP-CS-Fixer rules
20
PHP-CS-Fixer Rules
21
What's static code analysis?
22
Static Code Analysis
It's the analysis of computer software that is performed without actually
executing programs.
Dynamic code analysis is the analysis of computer software that is
performed by executing programs.
Unit tests, integration tests, system tests and acceptance tests use dynamic testing.
23
Static Code Analysis for PHP
Psalm
PHPStan
Phan→The PHP Father recommended
24
Installation
25
Installation
composer require phpstan/phpstan:0.* --dev
composer require vimeo/psalm:4.* --dev
composer require phan/phan:5.* --dev
26
Standard Checks
there are no syntax errors;
all the classes, methods, functions and constants exist;
the variables exist;
the hints in PHPDoc correspond to reality;
there are no arguments or variables unused.
Avoid copy-caste code errors and careless
27
Data type checks
Most analyzers allow to configure the level of strictness of checking and
imitate strict_types:
they check that String or Boolean aren’t passed to this function.
28
Union types
Most analyzers allow to configure the level of strictness of checking and
imitate strict_types:
they check that String or Boolean aren’t passed to this function.
/**
* @var string|int|bool $yes_or_no
*/
function isYes($yes_or_no) :bool
{
if (is_numeric($yes_or_no)) {
return $yes_or_no > 0;
} else {
return strtoupper($yes_or_no) == 'YES';
}
}
1
2
3
4
5
6
7
8
9
10
11
29
False type
Most analyzers allow to configure the level of strictness of checking and
imitate strict_types:
they check that String or Boolean aren’t passed to this function.
/** @return int|bool */
function fwrite(...) {
…
}
1
2
3
4
30
False type Error
<?php
/** @return resource|bool */
function open_file() {
$fp = fopen('./composer.json', 'r');
if($fp === false) {
return false;
}
return fwrite($fp, "some string");
}
1
2
3
4
5
6
7
8
9
10
11
lee@lee-VirtualBox:~/phpstan-example$ vendor/bin/phpstan analyse ./false_type.php --level=max -c phpstan
------ --------------------------------------------------------------------------------------------
Line false_type.php
------ --------------------------------------------------------------------------------------------
4 Function open_file() never returns resource so it can be removed from the return typehint.
10 Function open_file() should return bool|resource but returns int|false.
------ --------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
31
False type Error Fix
<?php
/** @return int|false */
function open_file() {
$fp = fopen('./composer.json', 'r');
if($fp === false) {
return false;
}
return fwrite($fp, "some string");
}
1
2
3
4
5
6
7
8
9
10
11
lee@lee-VirtualBox:~/phpstan-example$ vendor/bin/phpstan analyse ./false_type.php 
--level=max -c phpstan.neon --no-progress --ansi
[OK] No errors
1
2
3
4
32
Array shapes
<?php
/** @return array */
function array_func(array $arr) {
return $arr;
}
1
2
3
4
5
6
lee@lee-VirtualBox:~/phpstan-example$ vendor/bin/phpstan analyse ./array_example.php 
--level=max -c phpstan.neon --no-progress --ansi
------ -----------------------------------------------------------------------------------------------
Line array_example.php
------ -----------------------------------------------------------------------------------------------
4 Function array_func() has parameter $arr with no value type specified in iterable type array.
💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
4 Function array_func() return type has no value type specified in iterable type array.
💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
------ -----------------------------------------------------------------------------------------------
[ERROR] Found 2 errors
1
2
3
4
5
6
7
8
9
10
11
12
13
33
Array shapes fix
<?php
/**
@param array<string> $arr
@return array<string>
*/
function array_func($arr) {
return $arr;
}
1
2
3
4
5
6
7
8
9
34
Overview of static code analysis tools
35
PHPStan
Developed by
Install it (the simplest way is via Composer)
Configure it (optional)
Run it
Ondřej Mirtes
lee@lee-VirtualBox:~/phpstan-example$ vendor/bin/phpstan analyse ./array_example.php
1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
[OK] No errors
💡 Tip of the Day:
PHPStan is performing only the most basic checks.
You can pass a higher rule level through the --level option
(the default and current level is 0) to analyse code more thoroughly.
lee@lee-VirtualBox:~/phpstan-example$
1
2
3
4
5
6
7
8
9
10
11
12
13
14 36
PHPStan Key Features
PHPStan will try to autoload unknown classes.
If some classes are not autoloaded, it will not be able to find them and
will return an error.
If using magical methods via __call, __get, or __set, it can write a plug-in
for PHPStan.
In actual fact, PHPStan doesn’t only perform autoload in the case of
unknown classes, but it also does so for all classes.
Using for configuration.
 No support for its PHPDoc tags @phpstan-var, @phpstan-return etc.
PhpStan has a playground website .
neon-format
 https://phpstan.org
37
Phan
Developed by the Etsy company. First commits by Rasmus Lerdorf.
Requiring the php-ast extension.
Plugin example is available .
 Creating a  file.
Playground website is .
here
.phan/config.php
available
lee@lee-VirtualBox:~/phpstan-example$ php vendor/bin/phan array_example.php
analyze ████████████████████████████████████████████████████████████ 100.0% 29MB/29MB
lee@lee-VirtualBox:~/phpstan-example$ php vendor/bin/phan array_example.php
analyze ████████████████████████████████████████████████████████████ 100.0% 28MB/31MB
array_example.php:9 PhanSyntaxError syntax error, unexpected '}', expecting ';' (at column 1)
1
2
3
4
5
6
38
Psalm
Developed by the Vimeo company
Annotations code
XML format file about configuration
Type aliases
array
closure
union type (for example, several classes or a class and other types)
enum
39
psalm.xml
<?xml version="1.0"?>
<psalm
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
https://github.com/Innmind/XML/blob/develop/psalm.xml
40
vendor/bin/psalm
░░░░░░░E░░░░E░E░░░EE░░░░░░░░░░░E░░░░E░░░░░E░E░░
ERROR: ParamNameMismatch - src/Element/Element.php:131:54 - Argument 2 of InnmindXmlElementElement::
public function replaceChild(int $position, Node $node): Node
ERROR: ParamNameMismatch - src/Element/SelfClosingElement.php:36:54 - Argument 2 of InnmindXmlElement
public function replaceChild(int $position, Node $node): Node
ERROR: ParamNameMismatch - src/Node/CharacterData.php:43:54 - Argument 2 of InnmindXmlNodeCharacterD
public function replaceChild(int $position, Node $node): Node
ERROR: ParamNameMismatch - src/Node/Comment.php:43:54 - Argument 2 of InnmindXmlNodeComment::replace
public function replaceChild(int $position, Node $node): Node
ERROR: ParamNameMismatch - src/Node/Document.php:86:54 - Argument 2 of InnmindXmlNodeDocument::repla
public function replaceChild(int $position, Node $node): Node
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
https://github.com/Innmind/XML/issues/2
41
CI/CD examples
42
GitHub Workflow examples
1. Using Composer to install required development dependencies.
2. GithubAction for PHP-CS-Fixer.
3. PHP Static Analysis in Github Actions.
43
composer install
.......
psalm:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['7.4', '8.0']
name: 'Psalm'
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Dependencies
run: composer install
- name: Psalm
run: vendor/bin/psalm --shepherd
.......
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
44
GithubAction for PHP-CS-Fixer
45
PHP Static Analysis in Github Actions
46
Laravel framework integration
47
Psalm plugin for Laravel
48
nunomaduro/larastan
49
參考資料
Phan
PHPStan
Psalm
PHPDoc
PHPStan Rules
GithubAction for PHP-CS-Fixer
Psalm on GitHub Workflow
Psalm plugin for Laravel
PHP Static Analysis in Github Actions
larastan
50
Thanks!
51

More Related Content

What's hot

How Functions Work
How Functions WorkHow Functions Work
How Functions WorkSaumil Shah
 
How to deploy node to production
How to deploy node to productionHow to deploy node to production
How to deploy node to productionSean Hess
 
Last train to php 7
Last train to php 7Last train to php 7
Last train to php 7Damien Seguy
 
Caching and tuning fun for high scalability @ FrOSCon 2011
Caching and tuning fun for high scalability @ FrOSCon 2011Caching and tuning fun for high scalability @ FrOSCon 2011
Caching and tuning fun for high scalability @ FrOSCon 2011Wim Godden
 
Is your code ready for PHP 7 ?
Is your code ready for PHP 7 ?Is your code ready for PHP 7 ?
Is your code ready for PHP 7 ?Wim Godden
 
Static Analysis of PHP Code – IPC Berlin 2016
Static Analysis of PHP Code – IPC Berlin 2016Static Analysis of PHP Code – IPC Berlin 2016
Static Analysis of PHP Code – IPC Berlin 2016Rouven Weßling
 
What is the Joomla Framework and why do we need it?
What is the Joomla Framework and why do we need it?What is the Joomla Framework and why do we need it?
What is the Joomla Framework and why do we need it?Rouven Weßling
 
The why and how of moving to PHP 5.5/5.6
The why and how of moving to PHP 5.5/5.6The why and how of moving to PHP 5.5/5.6
The why and how of moving to PHP 5.5/5.6Wim Godden
 
PHP traits, treat or threat?
PHP traits, treat or threat?PHP traits, treat or threat?
PHP traits, treat or threat?Nick Belhomme
 
PHP, Under The Hood - DPC
PHP, Under The Hood - DPCPHP, Under The Hood - DPC
PHP, Under The Hood - DPCAnthony Ferrara
 
PHP 7.1 : elegance of our legacy
PHP 7.1 : elegance of our legacyPHP 7.1 : elegance of our legacy
PHP 7.1 : elegance of our legacyDamien Seguy
 
Mastering Namespaces in PHP
Mastering Namespaces in PHPMastering Namespaces in PHP
Mastering Namespaces in PHPNick Belhomme
 
Php7 hhvm and co
Php7 hhvm and coPhp7 hhvm and co
Php7 hhvm and coPierre Joye
 
When e-commerce meets Symfony
When e-commerce meets SymfonyWhen e-commerce meets Symfony
When e-commerce meets SymfonyMarc Morera
 
Preparing code for Php 7 workshop
Preparing code for Php 7 workshopPreparing code for Php 7 workshop
Preparing code for Php 7 workshopDamien Seguy
 
Introduction to Debuggers
Introduction to DebuggersIntroduction to Debuggers
Introduction to DebuggersSaumil Shah
 
PHP7 - The New Engine for old good train
PHP7 - The New Engine for old good trainPHP7 - The New Engine for old good train
PHP7 - The New Engine for old good trainXinchen Hui
 
Php 7 hhvm and co
Php 7 hhvm and coPhp 7 hhvm and co
Php 7 hhvm and coPierre Joye
 

What's hot (20)

How Functions Work
How Functions WorkHow Functions Work
How Functions Work
 
How to deploy node to production
How to deploy node to productionHow to deploy node to production
How to deploy node to production
 
Php extensions
Php extensionsPhp extensions
Php extensions
 
Last train to php 7
Last train to php 7Last train to php 7
Last train to php 7
 
Caching and tuning fun for high scalability @ FrOSCon 2011
Caching and tuning fun for high scalability @ FrOSCon 2011Caching and tuning fun for high scalability @ FrOSCon 2011
Caching and tuning fun for high scalability @ FrOSCon 2011
 
Is your code ready for PHP 7 ?
Is your code ready for PHP 7 ?Is your code ready for PHP 7 ?
Is your code ready for PHP 7 ?
 
Static Analysis of PHP Code – IPC Berlin 2016
Static Analysis of PHP Code – IPC Berlin 2016Static Analysis of PHP Code – IPC Berlin 2016
Static Analysis of PHP Code – IPC Berlin 2016
 
What is the Joomla Framework and why do we need it?
What is the Joomla Framework and why do we need it?What is the Joomla Framework and why do we need it?
What is the Joomla Framework and why do we need it?
 
The why and how of moving to PHP 5.5/5.6
The why and how of moving to PHP 5.5/5.6The why and how of moving to PHP 5.5/5.6
The why and how of moving to PHP 5.5/5.6
 
PHP traits, treat or threat?
PHP traits, treat or threat?PHP traits, treat or threat?
PHP traits, treat or threat?
 
PHP, Under The Hood - DPC
PHP, Under The Hood - DPCPHP, Under The Hood - DPC
PHP, Under The Hood - DPC
 
PHP 7.1 : elegance of our legacy
PHP 7.1 : elegance of our legacyPHP 7.1 : elegance of our legacy
PHP 7.1 : elegance of our legacy
 
Mastering Namespaces in PHP
Mastering Namespaces in PHPMastering Namespaces in PHP
Mastering Namespaces in PHP
 
Php7 hhvm and co
Php7 hhvm and coPhp7 hhvm and co
Php7 hhvm and co
 
When e-commerce meets Symfony
When e-commerce meets SymfonyWhen e-commerce meets Symfony
When e-commerce meets Symfony
 
Preparing code for Php 7 workshop
Preparing code for Php 7 workshopPreparing code for Php 7 workshop
Preparing code for Php 7 workshop
 
Introduction to Debuggers
Introduction to DebuggersIntroduction to Debuggers
Introduction to Debuggers
 
PHP7 - The New Engine for old good train
PHP7 - The New Engine for old good trainPHP7 - The New Engine for old good train
PHP7 - The New Engine for old good train
 
Php 7 hhvm and co
Php 7 hhvm and coPhp 7 hhvm and co
Php 7 hhvm and co
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 

Similar to 2021.laravelconf.tw.slides2

20 PHP Static Analysis and Documentation Generators #burningkeyboards
20 PHP Static Analysis and Documentation Generators #burningkeyboards20 PHP Static Analysis and Documentation Generators #burningkeyboards
20 PHP Static Analysis and Documentation Generators #burningkeyboardsDenis Ristic
 
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...Rouven Weßling
 
Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016
Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016 Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016
Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016 Alexander Lisachenko
 
PHP 7X New Features
PHP 7X New FeaturesPHP 7X New Features
PHP 7X New FeaturesThanh Tai
 
Php 5.6 vs Php 7 performance comparison
Php 5.6 vs Php 7 performance comparisonPhp 5.6 vs Php 7 performance comparison
Php 5.6 vs Php 7 performance comparisonTu Pham
 
PHP 8: What's New and Changed
PHP 8: What's New and ChangedPHP 8: What's New and Changed
PHP 8: What's New and ChangedAyesh Karunaratne
 
Php psr standard 2014 01-22
Php psr standard 2014 01-22Php psr standard 2014 01-22
Php psr standard 2014 01-22Võ Duy Tuấn
 
Psr - php standards recommendations
Psr - php standards recommendationsPsr - php standards recommendations
Psr - php standards recommendationsHà Anh Sơn
 
Exploit techniques - a quick review
Exploit techniques - a quick reviewExploit techniques - a quick review
Exploit techniques - a quick reviewCe.Se.N.A. Security
 
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerPragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerMarina Kolpakova
 
Php 7.2 compliance workshop php benelux
Php 7.2 compliance workshop php beneluxPhp 7.2 compliance workshop php benelux
Php 7.2 compliance workshop php beneluxDamien Seguy
 
The why and how of moving to php 8
The why and how of moving to php 8The why and how of moving to php 8
The why and how of moving to php 8Wim Godden
 
Using PHPStan with Laravel App
Using PHPStan with Laravel AppUsing PHPStan with Laravel App
Using PHPStan with Laravel AppMuhammad Shehata
 
Standards: Don't pee in the pool
Standards: Don't pee in the poolStandards: Don't pee in the pool
Standards: Don't pee in the poolDavid Yell
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming languageVasavi College of Engg
 
AVATAR : Fixing Semantic Bugs with Fix Patterns of Static Analysis Violations
AVATAR : Fixing Semantic Bugs with Fix Patterns of Static Analysis ViolationsAVATAR : Fixing Semantic Bugs with Fix Patterns of Static Analysis Violations
AVATAR : Fixing Semantic Bugs with Fix Patterns of Static Analysis ViolationsDongsun Kim
 

Similar to 2021.laravelconf.tw.slides2 (20)

20 PHP Static Analysis and Documentation Generators #burningkeyboards
20 PHP Static Analysis and Documentation Generators #burningkeyboards20 PHP Static Analysis and Documentation Generators #burningkeyboards
20 PHP Static Analysis and Documentation Generators #burningkeyboards
 
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
 
Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016
Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016 Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016
Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016
 
PHP 7X New Features
PHP 7X New FeaturesPHP 7X New Features
PHP 7X New Features
 
Php 5.6 vs Php 7 performance comparison
Php 5.6 vs Php 7 performance comparisonPhp 5.6 vs Php 7 performance comparison
Php 5.6 vs Php 7 performance comparison
 
PHP 8: What's New and Changed
PHP 8: What's New and ChangedPHP 8: What's New and Changed
PHP 8: What's New and Changed
 
Symfony3 w duecie z Vue.js
Symfony3 w duecie z Vue.jsSymfony3 w duecie z Vue.js
Symfony3 w duecie z Vue.js
 
Php psr standard 2014 01-22
Php psr standard 2014 01-22Php psr standard 2014 01-22
Php psr standard 2014 01-22
 
Continuous Quality Assurance
Continuous Quality AssuranceContinuous Quality Assurance
Continuous Quality Assurance
 
Psr - php standards recommendations
Psr - php standards recommendationsPsr - php standards recommendations
Psr - php standards recommendations
 
Exploit techniques - a quick review
Exploit techniques - a quick reviewExploit techniques - a quick review
Exploit techniques - a quick review
 
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the CompilerPragmatic Optimization in Modern Programming - Demystifying the Compiler
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
 
Php 7.2 compliance workshop php benelux
Php 7.2 compliance workshop php beneluxPhp 7.2 compliance workshop php benelux
Php 7.2 compliance workshop php benelux
 
The why and how of moving to php 8
The why and how of moving to php 8The why and how of moving to php 8
The why and how of moving to php 8
 
Using PHPStan with Laravel App
Using PHPStan with Laravel AppUsing PHPStan with Laravel App
Using PHPStan with Laravel App
 
Standards: Don't pee in the pool
Standards: Don't pee in the poolStandards: Don't pee in the pool
Standards: Don't pee in the pool
 
Python
PythonPython
Python
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 
Php
PhpPhp
Php
 
AVATAR : Fixing Semantic Bugs with Fix Patterns of Static Analysis Violations
AVATAR : Fixing Semantic Bugs with Fix Patterns of Static Analysis ViolationsAVATAR : Fixing Semantic Bugs with Fix Patterns of Static Analysis Violations
AVATAR : Fixing Semantic Bugs with Fix Patterns of Static Analysis Violations
 

More from LiviaLiaoFontech

More from LiviaLiaoFontech (10)

2021laravelconftwslides12
2021laravelconftwslides122021laravelconftwslides12
2021laravelconftwslides12
 
2021laravelconftwslides11
2021laravelconftwslides112021laravelconftwslides11
2021laravelconftwslides11
 
2021laravelconftwslides10
2021laravelconftwslides102021laravelconftwslides10
2021laravelconftwslides10
 
2021laravelconftwslides9
2021laravelconftwslides92021laravelconftwslides9
2021laravelconftwslides9
 
2021laravelconftwslides8
2021laravelconftwslides82021laravelconftwslides8
2021laravelconftwslides8
 
2021laravelconftwslides6
2021laravelconftwslides62021laravelconftwslides6
2021laravelconftwslides6
 
2021laravelconftwslides4
2021laravelconftwslides42021laravelconftwslides4
2021laravelconftwslides4
 
2021.laravelconf.tw.slides5
2021.laravelconf.tw.slides52021.laravelconf.tw.slides5
2021.laravelconf.tw.slides5
 
2021.laravelconf.tw.slides3
2021.laravelconf.tw.slides32021.laravelconf.tw.slides3
2021.laravelconf.tw.slides3
 
2021.laravelconf.tw.slides1
2021.laravelconf.tw.slides12021.laravelconf.tw.slides1
2021.laravelconf.tw.slides1
 

Recently uploaded

Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Recently uploaded (20)

Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

2021.laravelconf.tw.slides2

  • 1. Coding style, Static code analysis and PHP 1
  • 2. Outline About me What's Coding style? PSR-2與PSR-12程式碼⾵格標準。 What's static code analysis? PHPStan Psalm Phan CI/CD examples Laravel framework integration 2
  • 3. About me Peter Active open source contributor An associate engineer DevOps Back-end System Architecture Researching Web Application Security PHP, Python and JavaScript Smart Grid Technology (2017~2021) Database, Data platform architecture (2021~) GitHub 3
  • 4. What's coding style? AKA Programming style 4
  • 7. Coding style Founded by PHP-FIG PHP Framework Interop Group PSR-1 PSR-2 PSR-12 More standard docs https://www.php-fig.org https://github.com/php-fig https://github.com/php-fig/fig-standards/tree/master/accepted 7
  • 8. PSR-1 Overview Files MUST use only <?php and <?= tags. Files MUST use only UTF-8 without BOM for PHP code. Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both. Namespaces and classes MUST follow an "autoloading" PSR: [PSR-0, PSR- 4]. Class names MUST be declared in StudlyCaps. Class constants MUST be declared in all upper case with underscore separators. Method names MUST be declared in camelCase.   8
  • 9. PSR-2 Overview(Deprecated) Code MUST follow a "coding style guide" PSR [ ]. Code MUST use 4 spaces for indenting, not tabs. There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines SHOULD be 80 characters or less. There MUST be one blank line after the namespace declaration, and there MUST be one blank line after the block of use declarations. Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body. Opening braces for methods MUST go on the next line, and closing braces MUST go on the next line after the body. Visibility MUST be declared on all properties and methods; abstract and final MUST be declared before the visibility; static MUST be declared after the visibility. Control structure keywords MUST have one space after them; method and function calls MUST NOT. Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body. Opening parentheses for control structures MUST NOT have a space after them, and closing parentheses for control structures MUST NOT have a space before. PSR-1 9
  • 10. PSR-12 This specification extends, expands and replaces PSR-2, the coding style guide and requires adherence to PSR-1, the basic coding standard. 10
  • 11. 11
  • 14. PHP_CodeSniffer curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar chmod +x phpcs.phar mv phpcs.phar phpcs phpcs --help phpcs --standard=PSR2 src/ tests/ curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar chmod +x phpcbf.phar mv phpcbf.phar phpcbf phpcbf --help phpcbf --standard=PSR2 src/ tests/ 14
  • 15. phpcs --standard=PSR2 FILE: ...n-source-contributions/localized/src/Validation/LtValidation.php ---------------------------------------------------------------------- FOUND 1 ERROR AFFECTING 1 LINE ---------------------------------------------------------------------- 31 | ERROR | [x] Use single instead of double quotes for simple | | strings. ---------------------------------------------------------------------- PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY ---------------------------------------------------------------------- FILE: ...is/build/open-source-contributions/localized/tests/bootstrap.php ---------------------------------------------------------------------- FOUND 1 ERROR AFFECTING 1 LINE ---------------------------------------------------------------------- 15 | ERROR | [x] Use single instead of double quotes for simple | | strings. ---------------------------------------------------------------------- PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY ---------------------------------------------------------------------- FILE: ...n-source-contributions/localized/src/Validation/BrValidation.php ---------------------------------------------------------------------- FOUND 2 ERRORS AFFECTING 1 LINE ---------------------------------------------------------------------- 196 | ERROR | [x] Use single instead of double quotes for simple | | strings. 196 | ERROR | [x] Use single instead of double quotes for simple | | strings. ---------------------------------------------------------------------- PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY ---------------------------------------------------------------------- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 15
  • 18. phpcs.xml <?xml version="1.0"?> <ruleset name="Coding Standard"> <arg name="basepath" value="."/> <arg name="colors"/> <arg value="sp"/> <config name="ignore_warnings_on_exit" value="1"/> <file>./src</file> <file>./tests</file> <rule ref="PSR2"></rule> <!-- <rule ref="PSR12"></rule> --> <rule ref="Squiz.Commenting.ClassComment"> <exclude name="Squiz.Commenting.ClassComment.TagNotAllowed"/> <type>warning</type> <exclude-pattern>*/tests/</exclude-pattern> </rule> <rule ref="Squiz.Commenting.ClassComment.Missing"> <type>warning</type> </rule> <rule ref="Squiz.Commenting.FunctionComment.Missing"> <type>warning</type> <exclude-pattern>*/config/</exclude-pattern> / 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 phpcs.xml.dist 18
  • 19. PHP-CS-Fixer curl -OL https://cs.symfony.com/download/php-cs-fixer-v2.phar php php-cs-fixer-v2.phar fix --dry-run --format=txt --verbose --diff --diff- format=udiff --config=.cs.php curl -OL https://cs.symfony.com/download/php-cs-fixer-v3.phar php php-cs-fixer-v3.phar fix --dry-run --format=txt --verbose --diff --diff- format=udiff --config=.cs.php 19
  • 20. .cs.php <?php return PhpCsFixerConfig::create() ->setUsingCache(false) ->setRiskyAllowed(true) //->setCacheFile(__DIR__ . '/.php_cs.cache') ->setRules([ '@PSR1' => true, '@PSR2' => true, '@Symfony' => true, 'psr4' => true, 'yoda_style' => false, 'array_syntax' => ['syntax' => 'short'], 'list_syntax' => ['syntax' => 'short'], 'concat_space' => ['spacing' => 'one'], 'cast_spaces' => ['space' => 'none'], 'compact_nullable_typehint' => true, 'increment_style' => ['style' => 'post'], 'declare_equal_normalize' => ['space' => 'single'], 'no_short_echo_tag' => true, 'protected_to_private' => false, 'phpdoc_align' => false, 'phpdoc_add_missing_param_annotation' => ['only_untyped' => false], 'phpdoc_order' => true, // psr-5 'phpdoc_no_empty_return' => false, 'align multiline comment' => true, // psr-5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 PHP-CS-Fixer rules 20
  • 22. What's static code analysis? 22
  • 23. Static Code Analysis It's the analysis of computer software that is performed without actually executing programs. Dynamic code analysis is the analysis of computer software that is performed by executing programs. Unit tests, integration tests, system tests and acceptance tests use dynamic testing. 23
  • 24. Static Code Analysis for PHP Psalm PHPStan Phan→The PHP Father recommended 24
  • 26. Installation composer require phpstan/phpstan:0.* --dev composer require vimeo/psalm:4.* --dev composer require phan/phan:5.* --dev 26
  • 27. Standard Checks there are no syntax errors; all the classes, methods, functions and constants exist; the variables exist; the hints in PHPDoc correspond to reality; there are no arguments or variables unused. Avoid copy-caste code errors and careless 27
  • 28. Data type checks Most analyzers allow to configure the level of strictness of checking and imitate strict_types: they check that String or Boolean aren’t passed to this function. 28
  • 29. Union types Most analyzers allow to configure the level of strictness of checking and imitate strict_types: they check that String or Boolean aren’t passed to this function. /** * @var string|int|bool $yes_or_no */ function isYes($yes_or_no) :bool { if (is_numeric($yes_or_no)) { return $yes_or_no > 0; } else { return strtoupper($yes_or_no) == 'YES'; } } 1 2 3 4 5 6 7 8 9 10 11 29
  • 30. False type Most analyzers allow to configure the level of strictness of checking and imitate strict_types: they check that String or Boolean aren’t passed to this function. /** @return int|bool */ function fwrite(...) { … } 1 2 3 4 30
  • 31. False type Error <?php /** @return resource|bool */ function open_file() { $fp = fopen('./composer.json', 'r'); if($fp === false) { return false; } return fwrite($fp, "some string"); } 1 2 3 4 5 6 7 8 9 10 11 lee@lee-VirtualBox:~/phpstan-example$ vendor/bin/phpstan analyse ./false_type.php --level=max -c phpstan ------ -------------------------------------------------------------------------------------------- Line false_type.php ------ -------------------------------------------------------------------------------------------- 4 Function open_file() never returns resource so it can be removed from the return typehint. 10 Function open_file() should return bool|resource but returns int|false. ------ -------------------------------------------------------------------------------------------- 1 2 3 4 5 6 7 31
  • 32. False type Error Fix <?php /** @return int|false */ function open_file() { $fp = fopen('./composer.json', 'r'); if($fp === false) { return false; } return fwrite($fp, "some string"); } 1 2 3 4 5 6 7 8 9 10 11 lee@lee-VirtualBox:~/phpstan-example$ vendor/bin/phpstan analyse ./false_type.php --level=max -c phpstan.neon --no-progress --ansi [OK] No errors 1 2 3 4 32
  • 33. Array shapes <?php /** @return array */ function array_func(array $arr) { return $arr; } 1 2 3 4 5 6 lee@lee-VirtualBox:~/phpstan-example$ vendor/bin/phpstan analyse ./array_example.php --level=max -c phpstan.neon --no-progress --ansi ------ ----------------------------------------------------------------------------------------------- Line array_example.php ------ ----------------------------------------------------------------------------------------------- 4 Function array_func() has parameter $arr with no value type specified in iterable type array. 💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type 4 Function array_func() return type has no value type specified in iterable type array. 💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type ------ ----------------------------------------------------------------------------------------------- [ERROR] Found 2 errors 1 2 3 4 5 6 7 8 9 10 11 12 13 33
  • 34. Array shapes fix <?php /** @param array<string> $arr @return array<string> */ function array_func($arr) { return $arr; } 1 2 3 4 5 6 7 8 9 34
  • 35. Overview of static code analysis tools 35
  • 36. PHPStan Developed by Install it (the simplest way is via Composer) Configure it (optional) Run it Ondřej Mirtes lee@lee-VirtualBox:~/phpstan-example$ vendor/bin/phpstan analyse ./array_example.php 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% [OK] No errors 💡 Tip of the Day: PHPStan is performing only the most basic checks. You can pass a higher rule level through the --level option (the default and current level is 0) to analyse code more thoroughly. lee@lee-VirtualBox:~/phpstan-example$ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 36
  • 37. PHPStan Key Features PHPStan will try to autoload unknown classes. If some classes are not autoloaded, it will not be able to find them and will return an error. If using magical methods via __call, __get, or __set, it can write a plug-in for PHPStan. In actual fact, PHPStan doesn’t only perform autoload in the case of unknown classes, but it also does so for all classes. Using for configuration.  No support for its PHPDoc tags @phpstan-var, @phpstan-return etc. PhpStan has a playground website . neon-format  https://phpstan.org 37
  • 38. Phan Developed by the Etsy company. First commits by Rasmus Lerdorf. Requiring the php-ast extension. Plugin example is available .  Creating a  file. Playground website is . here .phan/config.php available lee@lee-VirtualBox:~/phpstan-example$ php vendor/bin/phan array_example.php analyze ████████████████████████████████████████████████████████████ 100.0% 29MB/29MB lee@lee-VirtualBox:~/phpstan-example$ php vendor/bin/phan array_example.php analyze ████████████████████████████████████████████████████████████ 100.0% 28MB/31MB array_example.php:9 PhanSyntaxError syntax error, unexpected '}', expecting ';' (at column 1) 1 2 3 4 5 6 38
  • 39. Psalm Developed by the Vimeo company Annotations code XML format file about configuration Type aliases array closure union type (for example, several classes or a class and other types) enum 39
  • 41. vendor/bin/psalm ░░░░░░░E░░░░E░E░░░EE░░░░░░░░░░░E░░░░E░░░░░E░E░░ ERROR: ParamNameMismatch - src/Element/Element.php:131:54 - Argument 2 of InnmindXmlElementElement:: public function replaceChild(int $position, Node $node): Node ERROR: ParamNameMismatch - src/Element/SelfClosingElement.php:36:54 - Argument 2 of InnmindXmlElement public function replaceChild(int $position, Node $node): Node ERROR: ParamNameMismatch - src/Node/CharacterData.php:43:54 - Argument 2 of InnmindXmlNodeCharacterD public function replaceChild(int $position, Node $node): Node ERROR: ParamNameMismatch - src/Node/Comment.php:43:54 - Argument 2 of InnmindXmlNodeComment::replace public function replaceChild(int $position, Node $node): Node ERROR: ParamNameMismatch - src/Node/Document.php:86:54 - Argument 2 of InnmindXmlNodeDocument::repla public function replaceChild(int $position, Node $node): Node 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 https://github.com/Innmind/XML/issues/2 41
  • 43. GitHub Workflow examples 1. Using Composer to install required development dependencies. 2. GithubAction for PHP-CS-Fixer. 3. PHP Static Analysis in Github Actions. 43
  • 44. composer install ....... psalm: runs-on: ubuntu-latest strategy: matrix: php-version: ['7.4', '8.0'] name: 'Psalm' steps: - name: Checkout uses: actions/checkout@v2 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-version }} extensions: mbstring, intl - name: Get Composer Cache Directory id: composer-cache run: echo "::set-output name=dir::$(composer config cache-files-dir)" - name: Cache dependencies uses: actions/cache@v2 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} restore-keys: ${{ runner.os }}-composer- - name: Install Dependencies run: composer install - name: Psalm run: vendor/bin/psalm --shepherd ....... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 44
  • 46. PHP Static Analysis in Github Actions 46
  • 48. Psalm plugin for Laravel 48
  • 50. 參考資料 Phan PHPStan Psalm PHPDoc PHPStan Rules GithubAction for PHP-CS-Fixer Psalm on GitHub Workflow Psalm plugin for Laravel PHP Static Analysis in Github Actions larastan 50