dsifford/academic-bloggers-toolkit

View on GitHub
src/php/utils.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php
/**
* Utility functions used throughout the backend.
*
* @package ABT
*/
 
declare(strict_types=1);
 
namespace ABT\Utils;
 
/**
* Safely adds JSON data into a page to be used by scripts.
*
* @param string $id A unique ID for the data.
* @param mixed $data The data to be JSON encoded.
* @throws \RuntimeException If the provided action is already done and over with.
*/
Expected 0 spaces between argument "$data" and closing bracket; 1 found
Expected 0 spaces between opening bracket and type hint "string"; 1 found
Opening brace should be on a new line
function add_json_script( string $id, $data ) {
Spaces must be used to indent lines; tabs are not allowed
$prefix = is_admin() ? 'admin_' : 'wp_';
Spaces must be used to indent lines; tabs are not allowed
$action = $prefix . 'footer';
Expected 0 spaces before closing bracket; 1 found
Spaces must be used to indent lines; tabs are not allowed
Expected 0 spaces after opening bracket; 1 found
Space after opening parenthesis of function call prohibited
if ( did_action( $action ) ) {
Spaces must be used to indent lines; tabs are not allowed
Expected 0 spaces before closing bracket; 1 found
Space after opening parenthesis of function call prohibited
throw new \RuntimeException( 'Action already fired for JSON data with ID ' . $id );
Spaces must be used to indent lines; tabs are not allowed
}
Spaces must be used to indent lines; tabs are not allowed
add_action(
Spaces must be used to indent lines; tabs are not allowed
$action,
Expected 0 spaces between argument "$data" and closing bracket; 1 found
Expected 0 spaces between opening bracket and argument "$id"; 1 found
Spaces must be used to indent lines; tabs are not allowed
function () use ( $id, $data ) {
Spaces must be used to indent lines; tabs are not allowed
?>
Spaces must be used to indent lines; tabs are not allowed
<script
Spaces must be used to indent lines; tabs are not allowed
Space after opening parenthesis of function call prohibited
Expected 0 spaces before closing bracket; 1 found
id="<?php echo esc_attr( $id ); ?>"
Spaces must be used to indent lines; tabs are not allowed
type="application/json"
Spaces must be used to indent lines; tabs are not allowed
Expected 0 spaces before closing bracket; 1 found
Space after opening parenthesis of function call prohibited
><?php echo wp_json_encode( $data ); ?></script>
Spaces must be used to indent lines; tabs are not allowed
<?php
Spaces must be used to indent lines; tabs are not allowed
}
Spaces must be used to indent lines; tabs are not allowed
);
}
 
/**
* Parses and returns ./citation-styles.json
*/
Opening brace should be on a new line
function get_citation_styles() {
Spaces must be used to indent lines; tabs are not allowed
return json_decode(
Spaces must be used to indent lines; tabs are not allowed
file_get_contents( // phpcs:ignore
Spaces must be used to indent lines; tabs are not allowed
ABT_ROOT_PATH . '/citation-styles.json'
Spaces must be used to indent lines; tabs are not allowed
)
Spaces must be used to indent lines; tabs are not allowed
);
}
 
/**
* Utility function that registers a script and/or its associated style if it exists.
*
* @param string $relpath Path of script/style relative to the bundle directory.
* @param array $extra_deps Optional. Array of extra handles to add as dependencies. Default [].
*/
Method `register_script` has 41 lines of code (exceeds 25 allowed). Consider refactoring.
Expected 0 spaces between argument "$extra_deps" and closing bracket; 1 found
Expected 0 spaces between opening bracket and type hint "string"; 1 found
Opening brace should be on a new line
function register_script( string $relpath, array $extra_deps = [] ) {
Spaces must be used to indent lines; tabs are not allowed
$handle = 'abt-' . $relpath;
Spaces must be used to indent lines; tabs are not allowed
$script_suffix = "/bundle/$relpath.js";
Spaces must be used to indent lines; tabs are not allowed
$style_suffix = "/bundle/$relpath.css";
 
Spaces must be used to indent lines; tabs are not allowed
$script_asset_path = ABT_ROOT_PATH . "/bundle/$relpath.asset.php";
Spaces must be used to indent lines; tabs are not allowed
Expected 0 spaces before closing bracket; 1 found
Space after opening parenthesis of function call prohibited
$script_asset = file_exists( $script_asset_path ) ? require $script_asset_path : [
Spaces must be used to indent lines; tabs are not allowed
'dependencies' => [],
Spaces must be used to indent lines; tabs are not allowed
Expected 0 spaces before closing bracket; 1 found
Space after opening parenthesis of function call prohibited
'version' => filemtime( ABT_ROOT_PATH . $script_suffix ),
Spaces must be used to indent lines; tabs are not allowed
];
Spaces must be used to indent lines; tabs are not allowed
Space after opening parenthesis of function call prohibited
Expected 0 spaces before closing bracket; 1 found
$script_asset['dependencies'] = array_merge( $script_asset['dependencies'], $extra_deps );
 
Expected 0 spaces before closing bracket; 1 found
Spaces must be used to indent lines; tabs are not allowed
Expected 0 spaces after opening bracket; 1 found
Space after opening parenthesis of function call prohibited
if ( file_exists( ABT_ROOT_PATH . $script_suffix ) ) {
Spaces must be used to indent lines; tabs are not allowed
wp_register_script(
Spaces must be used to indent lines; tabs are not allowed
$handle,
Spaces must be used to indent lines; tabs are not allowed
ABT_ROOT_URI . $script_suffix,
Spaces must be used to indent lines; tabs are not allowed
$script_asset['dependencies'],
Spaces must be used to indent lines; tabs are not allowed
$script_asset['version'],
Spaces must be used to indent lines; tabs are not allowed
true
Spaces must be used to indent lines; tabs are not allowed
);
Expected 0 spaces before closing bracket; 1 found
Spaces must be used to indent lines; tabs are not allowed
Space after opening parenthesis of function call prohibited
Expected 0 spaces after opening bracket; 1 found
if ( in_array( 'wp-i18n', $script_asset['dependencies'], true ) ) {
Spaces must be used to indent lines; tabs are not allowed
wp_set_script_translations(
Spaces must be used to indent lines; tabs are not allowed
$handle,
Spaces must be used to indent lines; tabs are not allowed
'academic-bloggers-toolkit',
Spaces must be used to indent lines; tabs are not allowed
Space after opening parenthesis of function call prohibited
Expected 0 spaces before closing bracket; 1 found
basename( ABT_ROOT_PATH ) . '/languages'
Spaces must be used to indent lines; tabs are not allowed
);
Spaces must be used to indent lines; tabs are not allowed
}
Spaces must be used to indent lines; tabs are not allowed
}
Expected 0 spaces before closing bracket; 1 found
Spaces must be used to indent lines; tabs are not allowed
Expected 0 spaces after opening bracket; 1 found
Space after opening parenthesis of function call prohibited
if ( file_exists( ABT_ROOT_PATH . $style_suffix ) ) {
Spaces must be used to indent lines; tabs are not allowed
$registered_styles = wp_styles()->registered;
Spaces must be used to indent lines; tabs are not allowed
wp_register_style(
Spaces must be used to indent lines; tabs are not allowed
$handle,
Spaces must be used to indent lines; tabs are not allowed
ABT_ROOT_URI . $style_suffix,
Spaces must be used to indent lines; tabs are not allowed
array_values(
Spaces must be used to indent lines; tabs are not allowed
array_filter(
Spaces must be used to indent lines; tabs are not allowed
$script_asset['dependencies'],
Expected 0 spaces between opening bracket and argument "$registered_styles"; 1 found
Expected 0 spaces between argument "$registered_styles" and closing bracket; 1 found
Expected 1 space after FUNCTION keyword; 0 found
Expected 0 spaces between opening bracket and argument "$id"; 1 found
Expected 0 spaces between argument "$id" and closing bracket; 1 found
Spaces must be used to indent lines; tabs are not allowed
function( $id ) use ( $registered_styles ) {
Spaces must be used to indent lines; tabs are not allowed
Expected 0 spaces before closing bracket; 1 found
Space after opening parenthesis of function call prohibited
return array_key_exists( $id, $registered_styles );
Spaces must be used to indent lines; tabs are not allowed
}
Spaces must be used to indent lines; tabs are not allowed
)
Spaces must be used to indent lines; tabs are not allowed
),
Spaces must be used to indent lines; tabs are not allowed
Expected 0 spaces before closing bracket; 1 found
Space after opening parenthesis of function call prohibited
filemtime( ABT_ROOT_PATH . $style_suffix )
Spaces must be used to indent lines; tabs are not allowed
);
Spaces must be used to indent lines; tabs are not allowed
}
}