macder /
wfv-validation
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
|||||||||||
|
0 ignored issues
–
show
|
||||||||||||
| 2 | defined( 'ABSPATH' ) or die(); |
|||||||||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Using logical operators such as
or instead of || is generally not recommended.
PHP has two types of connecting operators (logical operators, and boolean operators):
The difference between these is the order in which they are executed. In most cases,
you would want to use a boolean operator like Let’s take a look at a few examples: // Logical operators have lower precedence:
$f = false or true;
// is executed like this:
($f = false) or true;
// Boolean operators have higher precedence:
$f = false || true;
// is executed like this:
$f = (false || true);
Logical Operators are used for Control-FlowOne case where you explicitly want to use logical operators is for control-flow such as this: $x === 5
or die('$x must be 5.');
// Instead of
if ($x !== 5) {
die('$x must be 5.');
}
Since // The following is currently a parse error.
$x === 5
or throw new RuntimeException('$x must be 5.');
These limitations lead to logical operators rarely being of use in current PHP code. Loading history...
|
||||||||||||
| 3 | /* |
|||||||||||
| 4 | Plugin Name: WFV - Form Validation |
|||||||||||
| 5 | Plugin URI: https://macder.github.io/wfv/ |
|||||||||||
| 6 | Description: A simple fluid and concise API to manage user input, validation, feedback, and safe output. |
|||||||||||
| 7 | Version: 0.11.0 |
|||||||||||
| 8 | Author: Maciej Derulski |
|||||||||||
| 9 | Author URI: https://github.com/macder |
|||||||||||
| 10 | License: BSD 3-Clause |
|||||||||||
| 11 | License URI: https://github.com/macder/wp-form-validation/blob/master/LICENSE |
|||||||||||
| 12 | */ |
|||||||||||
| 13 | ||||||||||||
| 14 | define( 'WFV_VALIDATE_VERSION', '0.11.0' ); |
|||||||||||
| 15 | define( 'WFV_VALIDATE__MINIMUM_WP_VERSION', '3.7' ); |
|||||||||||
| 16 | define( 'WFV_VALIDATE__PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
|||||||||||
| 17 | define( 'WFV_VALIDATE__ACTION_POST', 'validate_form' ); |
|||||||||||
| 18 | ||||||||||||
| 19 | require_once WFV_VALIDATE__PLUGIN_DIR . '/vendor/autoload.php'; |
|||||||||||
| 20 | ||||||||||||
| 21 | use WFV\FormComposite; |
|||||||||||
| 22 | use WFV\Agent\InspectionAgent; |
|||||||||||
| 23 | use WFV\Artisan\Director; |
|||||||||||
| 24 | use WFV\Artisan\FormArtisan; |
|||||||||||
| 25 | use WFV\Factory\ValidatorFactory; |
|||||||||||
| 26 | ||||||||||||
| 27 | /** |
|||||||||||
| 28 | * |
|||||||||||
| 29 | * |
|||||||||||
| 30 | * @since 0.10.0 |
|||||||||||
| 31 | * |
|||||||||||
| 32 | * @param string $action |
|||||||||||
| 33 | * @param array $form Form arguments |
|||||||||||
| 34 | * @param bool $trim Trim whitespace from beginning and end of string |
|||||||||||
| 35 | */ |
|||||||||||
| 36 | function wfv_create( $action, array &$form, $trim = true ) { |
|||||||||||
|
0 ignored issues
–
show
wfv_create uses the super-global variable $_POST which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
Loading history...
|
||||||||||||
| 37 | $inspect = new InspectionAgent( $action ); |
|||||||||||
| 38 | $input = ( true === $inspect->safe_submit() ) ? $_POST : array(); |
|||||||||||
| 39 | ||||||||||||
| 40 | $builder = new FormArtisan( $form ); |
|||||||||||
| 41 | $form = ( new Director( $action ) ) |
|||||||||||
| 42 | ->with( 'input', [ $input, $trim ] ) |
|||||||||||
| 43 | ->with( 'rules' ) |
|||||||||||
| 44 | ->with( 'errors' ) |
|||||||||||
| 45 | ->with( 'messages' ) |
|||||||||||
| 46 | ->with( 'validator' ) |
|||||||||||
| 47 | ->compose( $builder ); |
|||||||||||
| 48 | ||||||||||||
| 49 | if( $input ) { |
|||||||||||
| 50 | wfv_validate( $form ); |
|||||||||||
| 51 | } |
|||||||||||
| 52 | } |
|||||||||||
| 53 | ||||||||||||
| 54 | /** |
|||||||||||
| 55 | * |
|||||||||||
| 56 | * |
|||||||||||
| 57 | * @since 0.11.0 |
|||||||||||
| 58 | * |
|||||||||||
| 59 | * @param FormComposite $form |
|||||||||||
| 60 | * @return bool |
|||||||||||
| 61 | */ |
|||||||||||
| 62 | function wfv_validate( FormComposite $form ) { |
|||||||||||
| 63 | $rules = $form->rules()->get_array(); |
|||||||||||
| 64 | $messages = $form->messages()->get_array(); |
|||||||||||
| 65 | $factory = new ValidatorFactory( $messages ); |
|||||||||||
| 66 | ||||||||||||
| 67 | foreach( $rules as $field => $ruleset ) { |
|||||||||||
| 68 | $optional = in_array('optional', $ruleset); |
|||||||||||
| 69 | if( $optional ) { |
|||||||||||
| 70 | array_shift( $ruleset ); |
|||||||||||
| 71 | } |
|||||||||||
| 72 | foreach( $ruleset as $rule ) { |
|||||||||||
| 73 | $params = ( is_array( $rule ) ) ? $rule['params'] : null; |
|||||||||||
| 74 | $rule_name = ( is_string( $rule ) ) ? $rule : $rule['rule']; |
|||||||||||
| 75 | $validator = $factory->create( $rule_name, $field, $params, $optional ); |
|||||||||||
| 76 | $form->validate( $validator, $field ); |
|||||||||||
| 77 | } |
|||||||||||
| 78 | } |
|||||||||||
| 79 | return $form->is_valid(); |
|||||||||||
| 80 | } |
|||||||||||
| 81 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.