Automattic / Automattic/VIP-Coding-Standards

Check files in the vip-config/ directory for WordPress functions

Đang mở
#467 2 bình luận 2 reaction 0 người được giao Xem trên GitHub
Standard: VIP-Go 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?

The VIP app repo has a [`vip-config/` directory](https://github.com/Automattic/vip-go-skeleton/tree/master/vip-config) which contains files that run before WordPress is loaded. Usually there's just a `vip-config/vip-config.php` file, but clients can create other files in this directory and include/require them from the default file.

Since WordPress is not loaded when these files are read, WordPress-defined functions are not available, but this can be easy to forget. Using a WP function when not defined, gives a fatal error which can take down a site.

## Describe the solution you'd like

A new sniff that can be applied to files in the `vip-config/` directory, and look for any functions that are not defined yet. We can use [`get_defined_functions()` function](https://www.php.net/manual/en/function.get-defined-functions.php) to look for defined functions. The `internal` index key will verify against PHP native functions, but we also need to consider functions that may be defined within the files of `vip-config/` itself. It may also depend on if a function call is done before or after the definition in the same file. There may be some false positives cases, but these are more likely to be edge cases vs capturing functions only defined in WordPress.

Early in the `process()`, use `$phpcs->getFilename()` which can give the complete file path, to abort the sniff early if not correct. The sniff should have a public property with the default pattern of files which should be scanned.

## What code should be reported as a violation?

```php
// In vip-config/vip-config.php:
home_url();
wp_redirect( ... );
// etc.
```

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

```php
// In vip-config/vip-config.php:
function foo() {
return 'foo';
}
foo();

bar();
function bar() {
return 'bar';
}
```

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

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

Hướng nghiên cứu

Start with the new sniff's process() and use $phpcs->getFilename() to confirm the public file pattern matches vip-config/vip-config.php; review get_defined_functions() and function definitions and calls in that file. Done means calls such as home_url() and wp_redirect() are reported, while functions defined in the same file, such as foo() and bar(), are not.

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, wordpress
Lĩnh vực
tooling
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
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
35/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.