Automattic / Automattic/VIP-Coding-Standards

IncludingFile: Support runtime setting for custom variables in paths.

Đang mở
#466 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Type: Enhancement
Ngôn ngữ chính
PHP
Star
261
Fork
44
Merge trung bình
19 phút
Pull request đã merge (30 ngày)
1

Mô tả

## What problem would the enhancement address for VIP?

When a file includes a load of other includes/requires, then part of the path might be assigned to a variable, to make code DRY.

e.g.

```
$my_path = 'path/to/includes/;
require $my_path . 'foo.php';
require $my_path . 'bar.php';
require $my_path . 'baz.php';
```

The [IncludingFileSniff](https://github.com/Automattic/VIP-Coding-Standards/blob/develop/WordPressVIPMinimum/Sniffs/Files/IncludingFileSniff.php) checks the path, and other than a few exclusions for common functions and constants, it throws warnings for any other constants, functions and variables.

Since the prefix is often consistent for the file, the bot will add comment for each line.

The arrays of known constants and functions are all in public properties, so they can be overridden (though not implemented in the best way - see https://github.com/Automattic/VIP-Coding-Standards/issues/234), but variables cannot.

## Describe the solution you'd like

How about we made the sniff look for a runtime setting comment that defined that a particular variable was OK / should be ignored for file inclusion?
It would be set it at the top before all the includes, do the includes, then unset it again after.

```php
$my_path = 'path/to/includes/;

// phpcs:set WordPressVIPMinimum.Files.IncludingFile custom_variables[] $my_path
require $my_path . 'foo.php';
require $my_path . 'bar.php';
require $my_path . 'baz.php';
// phpcs:set WordPressVIPMinimum.Files.IncludingFile custom_variables[]
```

The bot workflow should be able to follow it since it would be an inline comment, not in a PHPCS config file.

The same could already be done with the public properties for known functions and constants, but as can be seen from the snippet above, the ideal unsetting would need to include the current values, which is awkward. As such, public `custom*` properties that are merged with the existing properties would be better.

## What code should be reported as a violation?

No new code.

## What code should *not* be reported as a violation?

Same as example above, where a runtime setting matches the name of a variable.

## Anything else

Unit tests should also cover items like `require $my_path . $my_other_path . 'baz.php';` where there are multiple variables, or `require MY_CONSTANT . $my_path . 'baz.php';` where there are multiple custom items.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu với WordPressVIPMinimum/Sniffs/Files/IncludingFileSniff.php và theo dõi cách các cài đặt runtime cũng như các hằng số, hàm và biến đã biết hiện có được xử lý. Bổ sung phạm vi kiểm thử đơn vị cho một hoặc nhiều biến tùy chỉnh và các tổ hợp với một hằng số, trong đó cài đặt runtime được tài liệu hóa cho phép các đường dẫn include khớp, còn các biến khác vẫn là vi phạm.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
php
Lĩnh vực
tooling
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
38/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.