Hướng dẫn phpstan-ignore-line - phpstan-bỏ qua-dòng

Thực đơn

Bạn có thể muốn bỏ qua một số lỗi mà Phpstan tìm thấy vì nhiều lý do:

  • Lỗi có thể là một vấn đề thực sự cần phải tái cấu trúc cơ sở mã của bạn mà bạn hiện không có thời gian.
  • Một tiện ích mở rộng PHPSTAN phải được viết để làm cho PHPSTAN hiểu mã bị ảnh hưởng thực sự làm gì và bạn chọn không làm điều đó ngay bây giờ.
  • Nó có một lỗi PHPSTAN chính hãng và bạn không muốn (và không nên) chờ đợi một lỗi.

Bỏ qua mã bằng cách sử dụng PHPDocs #

Có thể bỏ qua các lỗi bên cạnh dòng mã vi phạm bằng cách sử dụng các thẻ PHPDOC trong nhận xét:

  • parameters:
    ignoreErrors:
    - '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
    - '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
    2
  • parameters:
    ignoreErrors:
    - '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
    - '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
    3

Tất cả các kiểu bình luận PHP (

parameters:
ignoreErrors:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
4,
parameters:
ignoreErrors:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
5,
parameters:
ignoreErrors:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
6) có thể được sử dụng.

function () {
/** @phpstan-ignore-next-line */
echo $foo;

echo $foo; /** @phpstan-ignore-line */

/* @phpstan-ignore-next-line */
echo $foo;

echo $foo; /* @phpstan-ignore-line */

// @phpstan-ignore-next-line
echo $foo;

echo $foo; // @phpstan-ignore-line
};

Bỏ qua trong tệp cấu hình #

Có thể bỏ qua các lỗi bằng cách thêm biểu thức chính quy vào tệp cấu hình dưới khóa

parameters:
ignoreErrors:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
7. Để bỏ qua một lỗi bằng một biểu thức thông thường trong toàn bộ dự án, hãy thêm một mục nhập chuỗi:

parameters:
ignoreErrors:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'

Để bỏ qua các lỗi chỉ bằng một biểu thức thông thường trong một tệp cụ thể, hãy thêm một mục nhập với các khóa

parameters:
ignoreErrors:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
8 hoặc
parameters:
ignoreErrors:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
9 và
parameters:
ignoreErrors:
-
message: '#Access to an undefined property [a-zA-Z0-9\\_]+::\$foo#'
path: some/dir/SomeFile.php
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
paths:
- some/dir/*
- other/dir/*
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
path: other/dir/AnotherFile.php
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
paths:
- some/foo/dir/*
- other/foo/dir/*
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
path: other/dir/DifferentFile.php
count: 2 # optional
- '#Other error to ignore everywhere#'
0 hoặc
parameters:
ignoreErrors:
-
message: '#Access to an undefined property [a-zA-Z0-9\\_]+::\$foo#'
path: some/dir/SomeFile.php
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
paths:
- some/dir/*
- other/dir/*
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
path: other/dir/AnotherFile.php
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
paths:
- some/foo/dir/*
- other/foo/dir/*
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
path: other/dir/DifferentFile.php
count: 2 # optional
- '#Other error to ignore everywhere#'
1. Các mẫu ký tự đại diện tương thích với PHP
parameters:
ignoreErrors:
-
message: '#Access to an undefined property [a-zA-Z0-9\\_]+::\$foo#'
path: some/dir/SomeFile.php
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
paths:
- some/dir/*
- other/dir/*
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
path: other/dir/AnotherFile.php
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
paths:
- some/foo/dir/*
- other/foo/dir/*
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
path: other/dir/DifferentFile.php
count: 2 # optional
- '#Other error to ignore everywhere#'
2 cũng được hỗ trợ. Bạn có thể chỉ định số lần lỗi được mong đợi bằng cách sử dụng
parameters:
ignoreErrors:
-
message: '#Access to an undefined property [a-zA-Z0-9\\_]+::\$foo#'
path: some/dir/SomeFile.php
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
paths:
- some/dir/*
- other/dir/*
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
path: other/dir/AnotherFile.php
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
paths:
- some/foo/dir/*
- other/foo/dir/*
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
path: other/dir/DifferentFile.php
count: 2 # optional
- '#Other error to ignore everywhere#'
3 (tùy chọn, chỉ áp dụng cho
parameters:
ignoreErrors:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
8 không phải
parameters:
ignoreErrors:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
9 và
parameters:
ignoreErrors:
-
message: '#Access to an undefined property [a-zA-Z0-9\\_]+::\$foo#'
path: some/dir/SomeFile.php
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
paths:
- some/dir/*
- other/dir/*
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
path: other/dir/AnotherFile.php
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
paths:
- some/foo/dir/*
- other/foo/dir/*
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
path: other/dir/DifferentFile.php
count: 2 # optional
- '#Other error to ignore everywhere#'
0, không phải
parameters:
ignoreErrors:
-
message: '#Access to an undefined property [a-zA-Z0-9\\_]+::\$foo#'
path: some/dir/SomeFile.php
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
paths:
- some/dir/*
- other/dir/*
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
path: other/dir/AnotherFile.php
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
paths:
- some/foo/dir/*
- other/foo/dir/*
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
path: other/dir/DifferentFile.php
count: 2 # optional
- '#Other error to ignore everywhere#'
1).

parameters:
ignoreErrors:
-
message: '#Access to an undefined property [a-zA-Z0-9\\_]+::\$foo#'
path: some/dir/SomeFile.php
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
paths:
- some/dir/*
- other/dir/*
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
path: other/dir/AnotherFile.php
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
paths:
- some/foo/dir/*
- other/foo/dir/*
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
path: other/dir/DifferentFile.php
count: 2 # optional
- '#Other error to ignore everywhere#'

Các đường dẫn tương đối trong các phím

parameters:
ignoreErrors:
-
message: '#Access to an undefined property [a-zA-Z0-9\\_]+::\$foo#'
path: some/dir/SomeFile.php
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
paths:
- some/dir/*
- other/dir/*
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
path: other/dir/AnotherFile.php
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
paths:
- some/foo/dir/*
- other/foo/dir/*
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
path: other/dir/DifferentFile.php
count: 2 # optional
- '#Other error to ignore everywhere#'
0 và
parameters:
ignoreErrors:
-
message: '#Access to an undefined property [a-zA-Z0-9\\_]+::\$foo#'
path: some/dir/SomeFile.php
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
paths:
- some/dir/*
- other/dir/*
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
path: other/dir/AnotherFile.php
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
paths:
- some/foo/dir/*
- other/foo/dir/*
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
path: other/dir/DifferentFile.php
count: 2 # optional
- '#Other error to ignore everywhere#'
1 được giải quyết dựa trên thư mục của tệp cấu hình.
1.

Tạo mục nhập Onporeerrors #

Công cụ hữu ích này sẽ tạo ra một mục mà bạn có thể đặt trong phần

3 trong tệp cấu hình của bạn. Nó liên quan đến sự phức tạp của việc viết một biểu thức thông thường phù hợp từ một chuỗi đơn giản và mã hóa biểu thức thông thường đó vào định dạng neon.

Thông báo lỗi

Đường dẫn tùy chọn

Sao chép những điều sau đây vào Phpstan của bạn.neon:

Một lỗi đã xảy ra. Vui lòng thử lại sau.

Đường cơ sở #

Nếu bạn muốn bỏ qua tất cả các lỗi hiện tại và chỉ tập trung vào mã mới và thay đổi kể từ bây giờ, hãy đi và tìm hiểu về tính năng cơ sở.

Báo cáo không sử dụng bỏ qua #

Nếu một số lỗi bị bỏ qua (cả từ cấu hình và PHPDOC) không xảy ra trong kết quả nữa, PHPSTAN sẽ cho bạn biết và bạn sẽ phải xóa mẫu khỏi cấu hình. Bạn có thể tắt hành vi này bằng cách đặt

4 thành 
5 trong cấu hình:

parameters:
reportUnmatchedIgnoredErrors: false

Bạn có thể bật/tắt báo cáo không sử dụng bỏ qua rõ ràng cho mỗi mục trong

6. Điều này ghi lại cài đặt toàn cầu 
4.

parameters:
reportUnmatchedIgnoredErrors: false
ignoreErrors:
- '#This message will not be reported as unmatched#'
- '#This message will not be reported as unmatched either#'
-
message: '#But this one will be reported#'
reportUnmatched: true
parameters:
reportUnmatchedIgnoredErrors: true
ignoreErrors:
- '#This message will be reported as unmatched#'
- '#This message will be reported as unmatched too#'
-
message: '#But this one will not be reported#'
reportUnmatched: false

Không bao gồm toàn bộ tệp #

Nếu cơ sở mã của bạn chứa một số tệp bị hỏng có mục đích (e. G. Để kiểm tra hành vi của ứng dụng của bạn trên các tệp có mã PHP không hợp lệ), bạn có thể loại trừ chúng bằng khóa

8. Mỗi mục được sử dụng làm mẫu cho hàm 
parameters:
ignoreErrors:
-
message: '#Access to an undefined property [a-zA-Z0-9\\_]+::\$foo#'
path: some/dir/SomeFile.php
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
paths:
- some/dir/*
- other/dir/*
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
path: other/dir/AnotherFile.php
-
messages:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFooBar\(\)#'
paths:
- some/foo/dir/*
- other/foo/dir/*
-
message: '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
path: other/dir/DifferentFile.php
count: 2 # optional
- '#Other error to ignore everywhere#'
2.

parameters:
excludePaths:
- tests/*/data/*

Đây là một lối tắt cho:

parameters:
excludePaths:
analyseAndScan:
- tests/*/data/*

Nếu cấu trúc thư mục của dự án của bạn trộn mã của riêng bạn (mã bạn muốn phân tích và sửa lỗi trong) và mã bên thứ ba (mà bạn đang sử dụng để khám phá các ký hiệu, nhưng không muốn phân tích), cấu trúc tệp có thể trông giống như đây:

├── phpstan.neon
└── src
    ├── foo.php
    ├── ...
    └── thirdparty
        └── bar.php

Trong trường hợp này, bạn muốn phân tích toàn bộ thư mục

parameters:
reportUnmatchedIgnoredErrors: false
0, nhưng muốn loại trừ
parameters:
reportUnmatchedIgnoredErrors: false
1 khỏi phân tích. Đây là cách cấu hình PHPSTAN:

parameters:
ignoreErrors:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
0

Ngoài ra, có thể có một thư mục

parameters:
reportUnmatchedIgnoredErrors: false
2 chứa các tệp mà bạn không muốn phân tích cũng như không sử dụng để khám phá các ký hiệu. Bạn có thể sửa đổi cấu hình để đạt được hiệu ứng đó:

parameters:
ignoreErrors:
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doFoo\(\)#'
- '#Call to an undefined method [a-zA-Z0-9\\_]+::doBar\(\)#'
1

Chỉnh sửa trang này trên github