Skip to content

Commit 385a6be

Browse files
committed
Improve code samples
1 parent 6dd5c7f commit 385a6be

File tree

1 file changed

+62
-18
lines changed

1 file changed

+62
-18
lines changed

AGENTS.md

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,45 +71,89 @@ Every file, function, class, method constant, and global variable must have an a
7171

7272
### PHP
7373

74-
Whenever possible, the most specific PHP type hints should be used, when compatible with the minimum version of PHP supported by WordPress, unless the `testVersion` config in `phpcs.xml.dist` is higher.
74+
Follow coding conventions in WordPress core. Namespaces are generally not used, as they are not normally used in WordPress core code. Procedural programming patterns are favored where classes play a supporting role, rather than everything being written in OOP.
7575

76-
When native PHP type cannot be used, PHPStan's [PHPDoc Types](https://phpstan.org/writing-php-code/phpdoc-types) should be used, including not only the basic types but also subtypes like `non-empty-string`, [integer ranges](https://phpstan.org/writing-php-code/phpdoc-types#integer-ranges), [general arrays](https://phpstan.org/writing-php-code/phpdoc-types#general-arrays), and especially [array shapes](https://phpstan.org/writing-php-code/phpdoc-types#array-shapes). The types should comply with PHPStan's level 10.
76+
Whenever possible, the most specific PHP type hints should be used, when compatible with the minimum version of PHP supported by WordPress, unless the `testVersion` config in `phpcs.xml.dist` is higher. When native PHP type cannot be used, PHPStan's [PHPDoc Types](https://phpstan.org/writing-php-code/phpdoc-types) should be used, including not only the basic types but also subtypes like `non-empty-string`, [integer ranges](https://phpstan.org/writing-php-code/phpdoc-types#integer-ranges), [general arrays](https://phpstan.org/writing-php-code/phpdoc-types#general-arrays), and especially [array shapes](https://phpstan.org/writing-php-code/phpdoc-types#array-shapes). The types should comply with PHPStan's level 10. The one exception for using PHP types is whenever a function is used as a filter. Since plugins can supply any value at all when filtering, use the expected type with a union to `mixed`. The first statement in the function in this case must always check the type, and if it is not the expected type, override it to be so.
7777

78-
The one exception for using PHP types is whenever a function is used as a filter. Since plugins can supply any value at all when filtering, it is important for the filtered value to always be `mixed`. The first statement in the function in this case must always check the type, and if it is not the expected type, override it to be so. For example:
78+
Never render HTML `SCRIPT` tags directly in HTML. Always use the relevant APIs in WordPress for adding scripts, including `wp_enqueue_script()`, `wp_add_inline_script()`, `wp_localize_script()`, `wp_print_script_tag()`, `wp_print_inline_script_tag()`, `wp_enqueue_script_module()` among others. Favor modules over classic scripts.
79+
80+
Here is an example PHP file with various conventions demonstrated.
7981

8082
```php
8183
/**
82-
* Filters foo.
84+
* Filtering functions for the Bar plugin.
8385
*
84-
* @param string|mixed $foo Foo.
85-
* @return string Foo.
86+
* @since n.e.x.t
87+
* @package Bar
8688
*/
87-
function filter_foo( $foo, int $post_id ): string {
88-
if ( ! is_string( $foo ) ) {
89-
$foo = '';
89+
90+
/**
91+
* Filters post title to be upper case.
92+
*
93+
* @since n.e.x.t
94+
*
95+
* @param string|mixed $title Title.
96+
* @param positive-int $post_id Post ID.
97+
* @return string Upper-cased title.
98+
*/
99+
function bar_filter_title_uppercase( $title, int $post_id ): string {
100+
if ( ! is_string( $title ) ) {
101+
$title = '';
90102
}
91103
/**
92104
* Because plugins do bad things.
93105
*
94-
* @var string $foo
106+
* @var string $title
95107
*/
96108

97-
// Filtering logic goes here.
98-
99-
return $foo;
109+
return strtoupper( $title );
100110
}
101-
add_filter( 'foo', 'filter_foo', 10, 2 );
111+
add_filter( 'the_title', 'bar_filter_title_uppercase', 10, 2 );
102112
```
103113

104-
All PHP files should have a namespace which coincides with the `@package` tag in the file's PHPDoc header.
105-
106114
### JavaScript
107115

108116
All JavaScript code should be written with JSDoc comments. All function parameters, return values, and other types should use [TypeScript in JSDoc](https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html).
109117

110-
JavaScript code is written using ES modules. This JS code must be runnable as-is without having to go through a build step, so it must be plain JavaScript and not TypeScript. The project _may_ also distribute minified versions of these JS files.
118+
JavaScript code should be written using ES modules. This JS code must be runnable as-is without having to go through a build step, so it must be plain JavaScript and not TypeScript. The project _may_ also distribute minified versions of these JS files.
119+
120+
Here's an example JS file:
111121

112-
Never render HTML `script` markup directly. Always use the relevant APIs in WordPress for adding scripts, including `wp_enqueue_script()`, `wp_add_inline_script()`, `wp_localize_script()`, `wp_print_script_tag()`, `wp_print_inline_script_tag()`, `wp_enqueue_script_module()` among others. Since script modules are used, new scripts should normally have a `type="module"` when printing via `wp_print_inline_script_tag()` and when an external script is used, then `wp_enqueue_script_module()` is preferred.
122+
```js
123+
/**
124+
* Foo module for Optimization Detective
125+
*
126+
* This extension optimizes the foo performance feature.
127+
*
128+
* @since n.e.x.t
129+
*/
130+
131+
export const name = 'Foo';
132+
133+
/**
134+
* @typedef {import("web-vitals").LCPMetric} LCPMetric
135+
* @typedef {import("../optimization-detective/types.ts").InitializeCallback} InitializeCallback
136+
* @typedef {import("../optimization-detective/types.ts").InitializeArgs} InitializeArgs
137+
*/
138+
139+
/**
140+
* Initializes extension.
141+
*
142+
* @since n.e.x.t
143+
*
144+
* @type {InitializeCallback}
145+
* @param {InitializeArgs} args Args.
146+
*/
147+
export async function initialize( { log, onLCP, extendRootData } ) {
148+
onLCP(
149+
( metric ) => {
150+
handleLCPMetric( metric, extendRootData, log );
151+
}
152+
);
153+
}
154+
155+
// ... function definition for handleLCPMetric omitted ...
156+
```
113157

114158
### Static Analysis Commands
115159

0 commit comments

Comments
 (0)