Các quy tắc nghiêm ngặt và quan điểm của Phpstan
Phpstan tập trung vào việc tìm lỗi trong mã của bạn. Nhưng trong PHP có rất nhiều thời gian trong cách viết công cụ. Kho lưu trữ này chứa các quy tắc bổ sung xoay quanh mã được gõ chặt chẽ và mạnh mẽ mà không có sự đúc lỏng nào cho những người muốn có thêm sự an toàn trong lập trình phòng thủ cực kỳ
- Yêu cầu Booleans trong
if
,elseif
, toán tử ternary, sau!
và ở cả hai phía của&&
và||
. - Yêu cầu các toán hạng hoặc mảng bằng số trong
0 và các toán tử số trong ________ 11/________ 12/________ 13/________ 14/________ 15.includes: - vendor/phpstan/phpstan-strict-rules/rules.neon
- Yêu cầu toán hạng số trong
6,includes: - vendor/phpstan/phpstan-strict-rules/rules.neon
7,includes: - vendor/phpstan/phpstan-strict-rules/rules.neon
8andincludes: - vendor/phpstan/phpstan-strict-rules/rules.neon
9.includes: - vendor/phpstan/phpstan-strict-rules/rules.neon
- Các chức năng này chứa tham số
parameters: strictRules: disallowedLooseComparison: false booleansInConditions: false uselessCast: false requireParentConstructorCall: false disallowedConstructs: false overwriteVariablesWithLoop: false closureUsesThis: false matchingInheritedMethodNames: false numericOperandsInArithmeticOperators: false strictCalls: false switchConditionsMatchingType: false noVariableVariables: false
0 để an toàn loại tốt hơn, nó phải được đặt thànhparameters: strictRules: disallowedLooseComparison: false booleansInConditions: false uselessCast: false requireParentConstructorCall: false disallowedConstructs: false overwriteVariablesWithLoop: false closureUsesThis: false matchingInheritedMethodNames: false numericOperandsInArithmeticOperators: false strictCalls: false switchConditionsMatchingType: false noVariableVariables: false
1:parameters: strictRules: disallowedLooseComparison: false booleansInConditions: false uselessCast: false requireParentConstructorCall: false disallowedConstructs: false overwriteVariablesWithLoop: false closureUsesThis: false matchingInheritedMethodNames: false numericOperandsInArithmeticOperators: false strictCalls: false switchConditionsMatchingType: false noVariableVariables: false
2 [tham số thứ 3]parameters: strictRules: disallowedLooseComparison: false booleansInConditions: false uselessCast: false requireParentConstructorCall: false disallowedConstructs: false overwriteVariablesWithLoop: false closureUsesThis: false matchingInheritedMethodNames: false numericOperandsInArithmeticOperators: false strictCalls: false switchConditionsMatchingType: false noVariableVariables: false
3 [tham số thứ 3]parameters: strictRules: disallowedLooseComparison: false booleansInConditions: false uselessCast: false requireParentConstructorCall: false disallowedConstructs: false overwriteVariablesWithLoop: false closureUsesThis: false matchingInheritedMethodNames: false numericOperandsInArithmeticOperators: false strictCalls: false switchConditionsMatchingType: false noVariableVariables: false
4 [tham số thứ 3; chỉ khi tham số thứ 2parameters: strictRules: disallowedLooseComparison: false booleansInConditions: false uselessCast: false requireParentConstructorCall: false disallowedConstructs: false overwriteVariablesWithLoop: false closureUsesThis: false matchingInheritedMethodNames: false numericOperandsInArithmeticOperators: false strictCalls: false switchConditionsMatchingType: false noVariableVariables: false
5 được cung cấp]parameters: strictRules: disallowedLooseComparison: false booleansInConditions: false uselessCast: false requireParentConstructorCall: false disallowedConstructs: false overwriteVariablesWithLoop: false closureUsesThis: false matchingInheritedMethodNames: false numericOperandsInArithmeticOperators: false strictCalls: false switchConditionsMatchingType: false noVariableVariables: false
6 [tham số thứ 2]
- Các biến được gán trong điều kiện vòng lặp
parameters: strictRules: disallowedLooseComparison: false booleansInConditions: false uselessCast: false requireParentConstructorCall: false disallowedConstructs: false overwriteVariablesWithLoop: false closureUsesThis: false matchingInheritedMethodNames: false numericOperandsInArithmeticOperators: false strictCalls: false switchConditionsMatchingType: false noVariableVariables: false
7 và gán vòng ban đầuparameters: strictRules: disallowedLooseComparison: false booleansInConditions: false uselessCast: false requireParentConstructorCall: false disallowedConstructs: false overwriteVariablesWithLoop: false closureUsesThis: false matchingInheritedMethodNames: false numericOperandsInArithmeticOperators: false strictCalls: false switchConditionsMatchingType: false noVariableVariables: false
8 không thể được sử dụng sau vòng lặp. - Các biến được đặt trong foreach luôn được lặp lại nhờ các mảng không trống không thể được sử dụng sau vòng lặp.
- Các loại trong điều kiện
parameters: strictRules: disallowedLooseComparison: false booleansInConditions: false uselessCast: false requireParentConstructorCall: false disallowedConstructs: false overwriteVariablesWithLoop: false closureUsesThis: false matchingInheritedMethodNames: false numericOperandsInArithmeticOperators: false strictCalls: false switchConditionsMatchingType: false noVariableVariables: false
9 và giá trịparameters: strictRules: allRules: false
0 phải khớp. PHP so sánh chúng một cách lỏng lẻo theo mặc định và điều đó có thể dẫn đến kết quả không mong muốn. - Kiểm tra xem các phương pháp được khai báo tĩnh được gọi là tĩnh.
- Không cho phép
parameters: strictRules: allRules: false
1 - Đó là một so sánh rất lỏng lẻo [xem hướng dẫn], nên sử dụng một so sánh nghiêm ngặt hơn. - Disallall cho phép vận hành ternary ngắn [
parameters: strictRules: allRules: false
2] - ngụ ý so sánh yếu, nên sử dụng toán tử Null Coalesce [parameters: strictRules: allRules: false
3] hoặc toán tử ternary có điều kiện nghiêm ngặt. - Không cho phép các biến biến [
parameters: strictRules: allRules: false
4,parameters: strictRules: allRules: false
5, v.v.] - Không cho phép ghi đè biến các biến với khóa và biến giá trị của Foreach
- Luôn luôn đúng
parameters: strictRules: allRules: false
6, kiểm tra loạiparameters: strictRules: allRules: false
7 Chức năng và so sánh nghiêm ngặt ________ 38/________ 39. Các kiểm tra này có thể được tắt bằng cách đặt ________ 40/________ 41/________ 42 thành FALSE. - Trường hợp chính xác cho tên chức năng được tham chiếu và gọi.
- Trường hợp chính xác cho tên phương thức được kế thừa và thực hiện.
- CONTRAVIANCE cho các loại tham số và hiệp phương sai cho các loại trả về trong các phương pháp di truyền [còn được gọi là nguyên tắc thay thế Liskov - LSP]
- Kiểm tra LSP ngay cả đối với các phương thức tĩnh
- Yêu cầu gọi hàm xây dựng cha mẹ
- Không cho phép sử dụng toán tử backtick [
parameters: strictRules: allRules: false booleansInConditions: true
3] - Đóng nên sử dụng
parameters: strictRules: allRules: false booleansInConditions: true
4 trực tiếp thay vì sử dụng biếnparameters: strictRules: allRules: false booleansInConditions: true
4 một cách gián tiếp
Các quy tắc bổ sung đang đến trong các bản phát hành tiếp theo!
Cài đặt
Để sử dụng tiện ích mở rộng này, yêu cầu nó trong nhà soạn nhạc:
composer require --dev phpstan/phpstan-strict-rules
Nếu bạn cũng cài đặt Phpstan/Extension-Installer thì tất cả bạn đều được thiết lập!
Hướng dẫn cài đặtNếu bạn không muốn sử dụng
parameters: strictRules: allRules: false booleansInConditions: true6, hãy bao gồm các quy tắc.
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
Vô hiệu hóa các quy tắc
Bạn có thể vô hiệu hóa các quy tắc bằng tham số cấu hình:
parameters: strictRules: disallowedLooseComparison: false booleansInConditions: false uselessCast: false requireParentConstructorCall: false disallowedConstructs: false overwriteVariablesWithLoop: false closureUsesThis: false matchingInheritedMethodNames: false numericOperandsInArithmeticOperators: false strictCalls: false switchConditionsMatchingType: false noVariableVariables: false
Cho phép các quy tắc từng người một
Nếu bạn không muốn bắt đầu sử dụng tất cả các quy tắc nghiêm ngặt có sẵn cùng một lúc nhưng chỉ có một hoặc hai, bạn có thể!
Bạn có thể vô hiệu hóa tất cả các quy tắc từ
parameters: strictRules: allRules: false booleansInConditions: true7 bao gồm với:
parameters: strictRules: allRules: false
Sau đó, bạn có thể kích hoạt lại các quy tắc riêng lẻ với các tham số cấu hình:
parameters: strictRules: allRules: false booleansInConditions: true