All Projects → johnbillion → args

johnbillion / args

Licence: GPL-2.0 License
Array arguments made bearable

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to args

phpstan-wordpress
WordPress extensions for PHPStan ⛏️
Stars: ✭ 182 (+156.34%)
Mutual labels:  phpstan
BetterPhpDocParser
[DEPRECATED] Moved to
Stars: ✭ 45 (-36.62%)
Mutual labels:  phpstan
noise-php
A starter-kit for your PHP project.
Stars: ✭ 52 (-26.76%)
Mutual labels:  phpstan
php-toolbox
🐳 A Docker image designed for PHP developers that care about code quality.
Stars: ✭ 18 (-74.65%)
Mutual labels:  phpstan
phpstan-webmozart-assert
PHPStan extension for webmozart/assert
Stars: ✭ 132 (+85.92%)
Mutual labels:  phpstan
phpstan-ga
GithubAction for PHPStan
Stars: ✭ 85 (+19.72%)
Mutual labels:  phpstan
phpstan-extensions
Extensions for PHPStan
Stars: ✭ 61 (-14.08%)
Mutual labels:  phpstan
phpstan-symfony
*DEPRECATED* Symfony extension for PHPStan
Stars: ✭ 42 (-40.85%)
Mutual labels:  phpstan
phpstan-dba
PHPStan based SQL static analysis and type inference for the database access layer
Stars: ✭ 163 (+129.58%)
Mutual labels:  phpstan
config
Config component, strictly typed
Stars: ✭ 14 (-80.28%)
Mutual labels:  strict-types
phpstan
PHP Static Analysis in Github Actions.
Stars: ✭ 41 (-42.25%)
Mutual labels:  phpstan
phpstan-enum
Enum class reflection extension for PHPStan
Stars: ✭ 42 (-40.85%)
Mutual labels:  phpstan
CI-Report-Converter
The tool converts different error reporting standards for deep compatibility with popular CI systems (TeamCity, IntelliJ IDEA, GitHub Actions, etc).
Stars: ✭ 17 (-76.06%)
Mutual labels:  phpstan
vim-phpstan
A Vim plugin for PHPStan - https://github.com/phpstan/phpstan. It calls `phpstan` to do static analysis of your PHP code and displays the errors in Vim's quickfix list.
Stars: ✭ 26 (-63.38%)
Mutual labels:  phpstan
strict-phpunit
Enables type-safe comparisons of objects in PHPUnit.
Stars: ✭ 18 (-74.65%)
Mutual labels:  strict-types
phpstan-phpspec
PhpSpec extension for PHPStan
Stars: ✭ 19 (-73.24%)
Mutual labels:  phpstan
woocommerce-stubs
WooCommerce function and class declaration stubs for static analysis.
Stars: ✭ 49 (-30.99%)
Mutual labels:  phpstan
Monofony
Main repository for all Monofony bundles
Stars: ✭ 47 (-33.8%)
Mutual labels:  phpstan
phpstan-custom
Specific PHPStan extensions
Stars: ✭ 15 (-78.87%)
Mutual labels:  phpstan
annotate-pull-request-from-checkstyle
cs2pr - Annotate a GitHub Pull Request based on a Checkstyle XML-report within your GitHub Action
Stars: ✭ 146 (+105.63%)
Mutual labels:  phpstan

Args

Many functions and methods in WordPress accept arguments as an associative array which your IDE or code editor cannot autocomplete like it does for individual function parameters.

$query = new WP_Query( [
	'post_type' => 'post',
	'category_something' => 'does this accept an integer or a string?',
	'number_of_...errr'
] );

This library provides well-documented classes which represent many of the associative array parameters used throughout WordPress. Using them at the point where you populate the arguments means you get autocompletion and intellisense in your code editor, and strict typing thanks to typed properties in PHP 7.4. Comprehensive types and constraints for PHPStan are also included.

Current Status

Last updated for WordPress 5.9.

Requirements

  • PHP 7.4 or PHP 8+

Installation

composer require johnbillion/args

Usage

Usage with a class constructor:

$args = new \Args\WP_Query;

$args->tag = 'amazing';
$args->posts_per_page = 100;

$query = new \WP_Query( $args->toArray() );

Usage with a procedural function parameter:

$args = new \Args\register_post_type;

$args->show_in_rest = true;
$args->taxonomies = [ 'genre', 'audience' ];

$story = register_post_type( 'story', $args->toArray() );

Meta Queries, Tax Queries, and Date Queries

The query classes in WordPress support variously meta_query, tax_query, and date_query arguments. These are fully supported and you can construct them in a structured and strongly typed way.

Creating a meta_query argument:

$args = new \Args\WP_Query;

// Create a clause
$clause = new \Args\MetaQuery\Clause;
$clause->key = 'my_meta_key';
$clause->value = 'my_meta_value';

// Add the clause
$args->meta_query->clauses[] = $clause;

$query = new \WP_Query( $args->toArray() );

Creating a tax_query argument:

$args = new \Args\WP_Query;

// Create a clause
$clause = new \Args\TaxQuery\Clause;
$clause->taxonomy = 'post_tag';
$clause->terms = [ 'amazing' ];

// Add the clause
$args->meta_query->clauses[] = $clause;

$query = new \WP_Query( $args->toArray() );

Creating a date_query argument:

$args = new \Args\WP_Query;

// Create a clause
$clause = new \Args\DateQuery\Clause;
$clause->year = 2000;
$clause->compare = '>=';

// Add the clause
$args->meta_query->clauses[] = $clause;

$query = new \WP_Query( $args->toArray() );

Alternatively you can construct a complete query object by calling the fromArray() static method with the same nested array syntax that WordPress core uses:

$args = new \Args\WP_Query;

// Set the meta query from an array
$array = [
	[
		'key' => 'my_meta_key',
		'value' => 'my_meta_value',
	]
];
$args->meta_query = $args->meta_query::fromArray( $array );

$query = new \WP_Query( $args->toArray() );

What's Provided

Posts

  • \Args\WP_Query
  • \Args\register_post_type
  • \Args\wp_insert_post
  • \Args\wp_update_post
  • \Args\get_posts
  • \Args\register_post_meta
  • \Args\register_post_status

Taxonomies and Terms

  • \Args\WP_Term_Query
  • \Args\register_taxonomy
  • \Args\wp_insert_term
  • \Args\wp_update_term
  • \Args\get_terms
  • \Args\get_categories
  • \Args\get_tags
  • \Args\register_term_meta
  • \Args\wp_count_terms
  • \Args\wp_get_object_terms
  • \Args\wp_dropdown_categories

Users

  • \Args\WP_User_Query
  • \Args\wp_insert_user
  • \Args\wp_update_user
  • \Args\get_users

Comments

  • \Args\WP_Comment_Query
  • \Args\get_comments

HTTP API

  • \Args\wp_remote_get
  • \Args\wp_remote_post
  • \Args\wp_remote_head
  • \Args\wp_remote_request
  • \Args\wp_safe_remote_get
  • \Args\wp_safe_remote_post
  • \Args\wp_safe_remote_head
  • \Args\wp_safe_remote_request

Everything Else

  • \Args\register_block_type
  • \Args\register_meta
  • \Args\register_rest_field
  • \Args\wp_get_nav_menus
  • \Args\wp_die

Type Checking

Typed class properties are implemented in this library where possible. If you pass a value of the wrong type to an argument that is typed, you'll get a fatal error as long as you're using strict types:

<?php
declare( strict_types=1 );

No more mystery bugs due to incorrect types.

Note that several parameters in WordPress accept multiple types, for example the $ignore_sticky_posts argument for \WP_Query can be a boolean or an integer. In some of these cases I've opted to type the parameter with the most appropriate type even though it can technically accept other types.

Static Analysis

PHPStan-specific @phpstan-var tags are used for properties that have a fixed set of values or other constraints. This allows for even greater type and value checking via static analysis with PHPStan.

Ensure you're using PHPStan 1.0 or higher to make the best use of these constraints.

Contributing

Check out CONTRIBUTING.md for information about generating your own Args definitions or contributing to the Args library.

But Why?

I have a name for these array-type parameters for passing arguments. I call them Stockholm Parameters. We've gotten so used to using them that we forget what a terrible design pattern it is. This library exists to work around the immediate issue without rearchitecting the whole of WordPress.

Sponsors

The time that I spend maintaining this library and others is in part sponsored by:

Automattic

Plus all my kind sponsors on GitHub:

Sponsors

Click here to find out about supporting this library and my other WordPress development tools and plugins.

License: GPLv2

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].