Skip to content

Commit e1943e0

Browse files
committed
✨ First rules
0 parents  commit e1943e0

Some content is hidden

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

53 files changed

+3679
-0
lines changed

.gitattributes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/.github export-ignore
2+
/tests export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
5+
/docker-compose.yml export-ignore
6+
/Dockerfile export-ignore
7+
/phpstan.neon export-ignore

.github/workflows/tests.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: tests
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
php-version: [ '8.0', '8.1', '8.2', '8.3', '8.4' ]
18+
19+
name: Tests on PHP ${{ matrix.php-version }}
20+
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
25+
- name: Setup PHP
26+
uses: shivammathur/setup-php@v2
27+
with:
28+
php-version: ${{ matrix.php-version }}
29+
coverage: none
30+
31+
- name: Validate composer.json and composer.lock
32+
run: composer validate
33+
34+
- name: Cache Composer packages
35+
id: composer-cache
36+
uses: actions/cache@v4
37+
with:
38+
path: vendor
39+
key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ hashFiles('**/composer.lock') }}
40+
restore-keys: |
41+
${{ runner.os }}-php-${{ matrix.php-version }}-
42+
43+
- name: Install dependencies
44+
if: steps.composer-cache.outputs.cache-hit != 'true'
45+
run: composer i --prefer-dist --no-progress
46+
47+
- name: Run test suite
48+
run: vendor/bin/phpunit

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
vendor
3+
.phpunit.result.cache

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM php:8.0
2+
3+
RUN apt update
4+
RUN apt install unzip curl -y
5+
6+
RUN curl -sS https://getcomposer.org/installer -o /usr/local/composer-setup.php
7+
8+
RUN php /usr/local/composer-setup.php --install-dir=/usr/local/bin --filename=composer
9+
10+
RUN rm /usr/local/composer-setup.php

README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# PHPStan Xefi Rules
2+
3+
[![CI state](https://img.shields.io/github/actions/workflow/status/xefi/phpstan-xefi-rules/tests.yml)](https://github.com/xefi/phpstan-xefi-rules)
4+
[![Packagist version](https://img.shields.io/packagist/v/xefi/phpstan-xefi-rules)](https://packagist.org/packages/xefi/phpstan-xefi-rules)
5+
6+
> Some custom PHPStan extensions to improve the readability, maintainability and overall quality of your PHP code base.
7+
8+
Package based on PHPStan. You can find the source package [here](https://phpstan.org/).
9+
10+
### Supported PHP versions
11+
| PHP version | This package version |
12+
|-------------|----------------------|
13+
| 7.4, 8.x | Latest |
14+
15+
## How to use it
16+
17+
**1**: First, you may use [Composer](https://getcomposer.org) to install this package as a development dependency into your project:
18+
19+
```bash
20+
composer require --dev "xefi/phpstan-xefi-rules"
21+
```
22+
23+
**2**: Then, create a `phpstan.neon` or `phpstan.neon.dist` file in the root of your application. It might look like this:
24+
25+
```
26+
includes:
27+
- vendor/xefi/phpstan-xefi-rules/extension.neon
28+
29+
parameters:
30+
paths:
31+
- src/
32+
33+
# 0 to 10, level 10 is the highest level
34+
level: 0
35+
```
36+
37+
The most important part is the `includes` one, that enables the rules of this package.
38+
39+
For all available options, please take a look at the PHPStan documentation: **https://phpstan.org/config-reference**
40+
41+
**3**: Finally, you may start analyzing your code using the phpstan console command:
42+
43+
```bash
44+
./vendor/bin/phpstan
45+
```
46+
47+
## Rules
48+
49+
### Boolean Property Naming Rule
50+
51+
*Identifier : xefi.booleanPropertyNaming*
52+
53+
Ensure that a Boolean is always named with an “is” at the beginning of its name, to clarify the condition it represents.
54+
55+
### Max Line Per Class Rule
56+
57+
*Identifier : xefi.maxLinePerClass*
58+
59+
Guarantee that a class is no more than 100 lines long, to clearly separate roles between classes and encourage clarification.
60+
61+
### Max Line Per Method Rule
62+
63+
*Identifier : xefi.maxLinePerMethod*
64+
65+
Guarantee that a method is no more than 20 lines long, to separate roles between classes and encourage specification.
66+
67+
### No Basic Exception Rule
68+
69+
*Identifier : xefi.noBasicException*
70+
71+
Ensure that no base exceptions are thrown in order to make custom and specific exceptions everywhere for better maintainability.
72+
73+
### No Delete Cascade Rule (Laravel)
74+
75+
*Identifier : xefi.noCascadeDeleteLaravel*
76+
77+
Prevents cascading deletions in SQL to avoid uncontrolled deletions.
78+
This rule forces you to think about the need for a cascading deletion, and to handle such cases in your code if you need to, which also makes it possible to use the observers and events managed by the framework.
79+
80+
This rule only works for Laravel.
81+
A Symfony rule is planned.
82+
83+
### No Generic Word Rule
84+
85+
*Identifier : xefi.noGenericWord*
86+
87+
Prevent generic variable names such as `array`, `string`, `str`, `result`, `res`, `data` to encourage developers to use clear and detailed names such as `users`, `filteredUsers`, `usersCount`, `cars`, ...
88+
89+
### No Laravel Observer Rule
90+
91+
*Identifier : xefi.noLaravelObserver*
92+
93+
Forbid the use of Observers in Laravel, as the behavior of observers is sometimes incomprehensible and uncontrollable.
94+
This rule encourages the use of Events & Listeners, whose behavior is similar but much more explicit and stable.
95+
96+
### No Try Catch Rule
97+
98+
*Identifier : xefi.noTryCatch*
99+
100+
Prevent `try catch` usage to ensure that the user uses PHP exceptions.
101+
This makes the code more readable and errors are handled by the framework if there is one.

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "xefi/phpstan-xefi-rules",
3+
"type": "phpstan-extension",
4+
"description": "Some custom PHPStan extensions for a better code base",
5+
"license": "MIT",
6+
"keywords": [
7+
"phpstan",
8+
"php",
9+
"rules"
10+
],
11+
"authors": [
12+
{
13+
"name": "Martin Soenen"
14+
}
15+
],
16+
"autoload": {
17+
"psr-4": {
18+
"Xefi\\PHPStanRules\\": "src/"
19+
}
20+
},
21+
"autoload-dev": {
22+
"psr-4": {
23+
"Xefi\\PHPStanRules\\Tests\\": "tests/"
24+
}
25+
},
26+
"require": {
27+
"php": ">=8.0",
28+
"phpstan/phpstan": "^2.1"
29+
},
30+
"require-dev": {
31+
"phpunit/phpunit": "^9.6"
32+
},
33+
"config": {
34+
"sort-packages": true
35+
}
36+
}

0 commit comments

Comments
 (0)