Бұл жазба автоматты түрде аударылған. Бастапқы тіл: Ағылшын
Larastan-Laravel мүмкіндіктерін қолдайтын статикалық кодты талдауға арналған PHPStan үстіндегі орауыш. Larastan пайдалану кодтың сапасын жақсартатын және қателерді түзетуге кететін шығындарды азайтатын кодтың ықтимал қателерін әзірлеудің бастапқы кезеңдерінде анықтауға мүмкіндік береді. Бұл нұсқаулықта біз Laravel қолданбасының сапасын 9-деңгейге көтеру үшін Larastan -. қалай орнатуға, конфигурациялауға және пайдалануға болатынын қарастырамыз.
Бірінші қадам-сіздің жобаңызға Larastan орнату. Бұл Composer көмегімен жасалады:
composer require larastan/larastan:^2.0 --dev
Бұл қадам larastan-ваш сіздің жобаңызға тәуелділік ретінде қосады. Осыдан кейін PHPStan конфигурация файлын жасау керек.
Жобаңыздың түбірінде phpstan файлын жасаңыз.neon. Бұл файлда PHPStan және Larastan үшін конфигурация болады:
includes:
- vendor/larastan/larastan/extension.neon
parameters:
paths:
- app/
level: 0
Бұл файлда біз larastan кеңейтімін қолданатынымызды және кодты тексерудің бастапқы деңгейін орнататынымызды көрсетеміз.
Енді біз бірінші кодты талдауды бастай аламыз:
./vendor/bin/phpstan analyse
Бұл талдау сіздің кодыңыздағы қателер мен ескертулерді көрсетеді. Бастапқы деңгейде (0-деңгей) проблемалардың ең аз саны анықталады. Сіздің міндетіңіз-деңгейге көтерілмес бұрын анықталған барлық қателіктерді түзету.
Larastan және PHPStan талдау деңгейлерін 0-ден 9-ға дейін қолдайды. Әр деңгей кодты тексерудің қатаң ережелерін қамтиды. Ағымдағы деңгейдегі барлық қателерді түзеткеннен кейін конфигурация файлындағы деңгейді көтеруге болады:
peopcopy codparameters:
level: 1
Деңгей өзгергеннен кейін талдауды қайта бастаңыз және анықталған қателерді түзетіңіз. Бұл процесті 9-деңгейге жеткенше қайталаңыз.
Негізгі тексеруден басқа, Larastan код сапасын жақсарту үшін қосымша мүмкіндіктер ұсынады:
- Түрлерді талдау: Larastan деректер түрлерін тексереді, бұл түрлерді дұрыс пайдаланбауға байланысты қателерді болдырмауға көмектеседі.
- Анотацияны қолдау: Larastan сіздің кодыңыздағы @var, @param, @return сияқты анотацияларды түсінеді және оларды түрлердің сәйкестігін тексеру үшін пайдаланады.
- Эталондық Интеграция: Larastan сіздің тесттеріңізбен біріктірілуі мүмкін, бұл тесттер орындалмай тұрып қателерді анықтауға мүмкіндік береді.
Larastan кодты жақсартуға қалай көмектесетіні туралы бірнеше мысалды қарастырыңыз.
function add(int $a, int $b): int {
return $a + $b;
}
echo add(1, '2');
Бұл мысалда функция int түрінің екі параметрін күтеді, бірақ екінші параметр жол ретінде беріледі. Larastan бұл қатені анықтап, ЕСКЕРТЕДІ.
/**
* @param int $a
* @param int $b
* @return int
*/
function add($a, $b) {
return $a + $b;
}
Егер сіз функциядағы параметрлердің түрлерін қосуды ұмытып қалсаңыз, Larastan анотацияларды тексереді және түрлердің сәйкессіздігін көрсетеді.
Larastan-вашем Laravel - жобаңызда пайдалану кодтың сапасын айтарлықтай жақсартуға мүмкіндік береді, қателерді әзірлеудің бастапқы кезеңдерінде анықтайды. Тексеру деңгейін 0-ден 9-ға дейін дәйекті түрде көтеру кодты сенімді және қауіпсіз етуге көмектеседі.
Larastan is a wrapper over PHPStan designed for static code analysis that supports Laravel features. Using Larastan allows you to identify potential errors in the code at the early stages of development, which improves the quality of the code and reduces the cost of fixing bugs. In this guide, we will look at how to install, configure and use Larastan to improve the quality of your Laravel application to Level 9.
The first step is to install Larastan in your project. This is done using Composer:
composer require larastan/larastan:^2.0 --dev
This step will add Larastan as a development dependency to your project. After that, you need to create a PHPStan configuration file.
Create a phpstan.neon file in the root of your project. This file will contain the configuration for PHPStan and Larastan:
includes:
- vendor/larastan/larastan/extension.neon
parameters:
paths:
- app/
level: 0
In this file, we specify that we will use the Larastan extension and set the initial level of code verification.
Now we can run the first code analysis:
./vendor/bin/phpstan analyse
This analysis will show errors and warnings in your code. At the initial level (level 0), a minimum number of problems will be identified. Your task is to correct all identified errors before you level up.
Larastan and PHPStan support analysis levels from 0 to 9. Each level includes stricter code validation rules. After you fix all the errors at the current level, you can upgrade the level in the configuration file:
Copy the parameters code:
level: 1
After changing the level, run the analysis again and correct the identified errors. Repeat this process until you reach level 9.
In addition to basic verification, Larastan offers additional features to improve the quality of the code:
- Type analysis: Larastan verifies data types, which helps to avoid errors related to incorrect use of types.
- Support for anotations: Larastan understands anotations in your code, such as @var, @param, @return, and uses them to verify type matching.
- Integration with tests: Larastan can be integrated with your tests, which allows you to identify errors even before running tests.
Let's look at some examples of how Larastan can help improve your code.
function add(int $a, int $b): int {
return $a + $b;
}
echo add(1, '2');
In this example, the function expects two int type parameters, but the second parameter is passed as a string. Larastan will identify this error and warn you.
/**
* @param int $a
* @param int $b
* @return int
*/
function add($a, $b) {
return $a + $b;
}
If you forget to specify the parameter types in the function, Larastan will check the annotations and point out the type mismatch.
Using Larastan in your Laravel project allows you to significantly improve the quality of the code by identifying errors in the early stages of development. Consistently increasing the verification level from 0 to 9 will help you make the code more reliable and secure.