Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cf13bd7

Browse files
author
dmitrydymarhchuk
committed
add-operands-in-ternary-operator-rule without checking the utility of the operator
2 parents 0edc460 + 9b86e1e commit cf13bd7

File tree

71 files changed

+2210
-747
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2210
-747
lines changed

‎.travis.yml‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
language: php
2+
3+
cache:
4+
directories:
5+
- $HOME/.composer/cache
6+
27
php:
38
- 7.1
49
- 7.2
10+
- 7.3
511
before_script:
6-
- composer self-update
712
- composer install
813
script:
914
- vendor/bin/phing
10-
- >
11-
wget https://github.com/maglnet/ComposerRequireChecker/releases/download/0.2.1/composer-require-checker.phar
12-
&& php composer-require-checker.phar check composer.json

‎LICENSE‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Ondřej Mirtes
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

‎README.md‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Extra strict and opinionated rules for PHPStan
22

3-
[![Build Status](https://travis-ci.org/phpstan/phpstan-strict-rules.svg)](https://travis-ci.org/phpstan/phpstan-strict-rules)
3+
[![Build Status](https://travis-ci.com/phpstan/phpstan-strict-rules.svg?branch=master)](https://travis-ci.com/phpstan/phpstan-strict-rules)
44
[![Latest Stable Version](https://poser.pugx.org/phpstan/phpstan-strict-rules/v/stable)](https://packagist.org/packages/phpstan/phpstan-strict-rules)
55
[![License](https://poser.pugx.org/phpstan/phpstan-strict-rules/license)](https://packagist.org/packages/phpstan/phpstan-strict-rules)
66

7-
[PHPStan](https://github.com/phpstan/phpstan) focuses on finding bugs in your code. But in PHP there's a lot of leeway in how stuff can be written. This repository contains additional rules that revolve around strictly and strongly typed code with no loose casting for those who want additional safety in extremely defensive programming:
7+
[PHPStan](https://phpstan.org/) focuses on finding bugs in your code. But in PHP there's a lot of leeway in how stuff can be written. This repository contains additional rules that revolve around strictly and strongly typed code with no loose casting for those who want additional safety in extremely defensive programming:
88

99
* Require booleans in `if`, `elseif`, ternary operator, after `!`, and on both sides of `&&` and `||`.
1010
* Require numeric operands or arrays in `+` and numeric operands in `-`/`*`/`/`/`**`/`%`.
@@ -15,30 +15,46 @@
1515
* `array_keys` (3rd parameter; only if the 2nd parameter `$search_value` is provided)
1616
* `base64_decode` (2nd parameter)
1717
* Variables assigned in `while` loop condition and `for` loop initial assignment cannot be used after the loop.
18+
* Variables set in foreach that's always looped thanks to non-empty arrays cannot be used after the loop.
1819
* Types in `switch` condition and `case` value must match. PHP compares them loosely by default and that can lead to unexpected results.
19-
* Statically declared methods are called statically.
20+
* Check that statically declared methods are called statically.
2021
* Disallow `empty()` - it's a very loose comparison (see [manual](https://php.net/empty)), it's recommended to use more strict one.
22+
* Disallow short ternary operator (`?:`) - implies weak comparison, it's recommended to use null coalesce operator (`??`) or ternary operator with strict condition.
23+
* Disallow variable variables (`$$foo`, `$this->$method()` etc.)
24+
* Disallow overwriting variables with foreach key and value variables
2125
* Always true `instanceof`, type-checking `is_*` functions and strict comparisons `===`/`!==`. These checks can be turned off by setting `checkAlwaysTrueInstanceof`/`checkAlwaysTrueCheckTypeFunctionCall`/`checkAlwaysTrueStrictComparison` to false.
22-
* Require parameter and return typehints for functions and methods (either native or phpDocs)
2326
* Correct case for referenced and called function names.
2427
* Correct case for inherited and implemented method names.
28+
* Contravariance for parameter types and covariance for return types in inherited methods (also known as Liskov substitution principle - LSP)
29+
* Check LSP even for static methods
30+
* Check missing typehint in anonymous function when a native one could be added
31+
* Require calling parent constructor
32+
* Disallow usage of backtick operator (`` $ls = `ls -la` ``)
2533

2634
Additional rules are coming in subsequent releases!
2735

28-
## Usage
2936

30-
To use these rules, require it in [Composer](https://getcomposer.org/):
37+
## Installation
38+
39+
To use this extension, require it in [Composer](https://getcomposer.org/):
3140

3241
```
3342
composer require --dev phpstan/phpstan-strict-rules
3443
```
3544

36-
And include rules.neon in your project's PHPStan config:
45+
If you also install [phpstan/extension-installer](https://github.com/phpstan/extension-installer) then you're all set!
46+
47+
<details>
48+
<summary>Manual installation</summary>
49+
50+
If you don't want to use `phpstan/extension-installer`, include rules.neon in your project's PHPStan config:
3751

3852
```
3953
includes:
40-
- vendor/phpstan/phpstan-strict-rules/rules.neon
54+
- vendor/phpstan/phpstan-strict-rules/rules.neon
4155
```
56+
</details>
57+
4258

4359
## Enabling rules one-by-one
4460

@@ -56,3 +72,5 @@ services:
5672
tags:
5773
- phpstan.rules.rule
5874
```
75+
76+
*Unfortunately, you cannot use phpstan/extension-installer in this case.*

‎build.xml‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,28 @@
22
<project name="Extra strict and opinionated rules for PHPStan" default="check">
33

44
<target name="check" depends="
5-
composer,
5+
composer-validate,
6+
composer-install,
67
lint,
78
cs,
9+
composer-normalize-check,
810
tests,
911
phpstan
1012
"/>
1113

12-
<target name="composer">
14+
<target name="composer-validate">
15+
<exec
16+
executable="composer"
17+
logoutput="true"
18+
passthru="true"
19+
checkreturn="true"
20+
>
21+
<arg value="validate"/>
22+
<arg value="--ansi"/>
23+
</exec>
24+
</target>
25+
26+
<target name="composer-install">
1327
<exec
1428
executable="composer"
1529
logoutput="true"
@@ -20,6 +34,31 @@
2034
</exec>
2135
</target>
2236

37+
<target name="composer-normalize-check">
38+
<exec
39+
executable="composer"
40+
logoutput="true"
41+
passthru="true"
42+
checkreturn="true"
43+
>
44+
<arg value="normalize"/>
45+
<arg value="--ansi"/>
46+
<arg value="--dry-run"/>
47+
</exec>
48+
</target>
49+
50+
<target name="composer-normalize-fix">
51+
<exec
52+
executable="composer"
53+
logoutput="true"
54+
passthru="true"
55+
checkreturn="true"
56+
>
57+
<arg value="normalize"/>
58+
<arg value="--ansi"/>
59+
</exec>
60+
</target>
61+
2362
<target name="lint">
2463
<exec
2564
executable="vendor/bin/parallel-lint"
@@ -88,7 +127,7 @@
88127
>
89128
<arg value="analyse"/>
90129
<arg value="-l"/>
91-
<arg value="7"/>
130+
<arg value="8"/>
92131
<arg value="-c"/>
93132
<arg path="phpstan.neon"/>
94133
<arg path="src"/>

‎composer.json‎

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,47 @@
11
{
22
"name": "phpstan/phpstan-strict-rules",
3+
"type": "phpstan-extension",
34
"description": "Extra strict and opinionated rules for PHPStan",
4-
"license": ["MIT"],
5-
"minimum-stability": "dev",
6-
"prefer-stable": true,
7-
"extra": {
8-
"branch-alias": {
9-
"dev-master": "0.10-dev"
10-
}
11-
},
5+
"license": [
6+
"MIT"
7+
],
128
"require": {
13-
"php": "~7.1",
14-
"phpstan/phpstan": "^0.10",
15-
"nikic/php-parser": "^4.0"
9+
"php": "^7.1 || ^8.0",
10+
"phpstan/phpstan": "^0.12.33"
1611
},
1712
"require-dev": {
1813
"consistence/coding-standard": "^3.0.1",
19-
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.4",
14+
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
15+
"ergebnis/composer-normalize": "^2.0.2",
2016
"jakub-onderka/php-parallel-lint": "^1.0",
2117
"phing/phing": "^2.16.0",
22-
"phpstan/phpstan-phpunit": "^0.10",
18+
"phpstan/phpstan-phpunit": "^0.12",
2319
"phpunit/phpunit": "^7.0",
2420
"slevomat/coding-standard": "^4.5.2"
2521
},
22+
"config": {
23+
"sort-packages": true
24+
},
25+
"extra": {
26+
"branch-alias": {
27+
"dev-master": "0.12-dev"
28+
},
29+
"phpstan": {
30+
"includes": [
31+
"rules.neon"
32+
]
33+
}
34+
},
2635
"autoload": {
2736
"psr-4": {
2837
"PHPStan\\": "src/"
2938
}
3039
},
3140
"autoload-dev": {
32-
"classmap": ["tests/"],
33-
"files": [
34-
"tests/Rules/Functions/data/missing-function-parameter-typehint.php",
35-
"tests/Rules/Functions/data/missing-function-return-typehint.php"
41+
"classmap": [
42+
"tests/"
3643
]
37-
}
44+
},
45+
"minimum-stability": "dev",
46+
"prefer-stable": true
3847
}

‎phpcs.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
@requires
2525
"/>
2626
</properties>
27+
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification"/>
28+
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification"/>
2729
</rule>
2830
<rule ref="SlevomatCodingStandard.ControlStructures.AssignmentInCondition"/>
2931
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowEqualOperators"/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /