push_options accesses the super-global variable $_POST. Open
function push_options() {
if ( empty( $_POST ) ) {
return false;
}
- Read upRead up
- Exclude checks
Superglobals
Since: 0.2
Accessing a super-global variable directly is considered a bad practice. These variables should be encapsulated in objects that are provided by a framework, for instance.
Example
class Foo {
public function bar() {
$name = $_POST['foo'];
}
}
Source
Reduce the number of returns of this function 4, down to the maximum allowed 3. Open
function push_options() {
- Read upRead up
- Exclude checks
Having too many return statements in a function increases the function's essential complexity because the flow of execution is broken each time a return statement is encountered. This makes it harder to read and understand the logic of the function.
Noncompliant Code Example
With the default threshold of 3:
function myFunction(){ // Noncompliant as there are 4 return statements if (condition1) { return true; } else { if (condition2) { return false; } else { return true; } } return false; }
Define a constant instead of duplicating this literal "\push_options" 4 times. Open
cargo()->action( 'wp_after_admin_bar_render', __NAMESPACE__ . '\\push_options' );
- Read upRead up
- Exclude checks
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() { prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times execute('action1'); release('action1'); }
Compliant Solution
ACTION_1 = 'action1'; function run() { prepare(ACTION_1); execute(ACTION_1); release(ACTION_1); }
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Similar blocks of code found in 2 locations. Consider refactoring. Open
<?php
namespace Isotop\Cargo\Admin;
use Isotop\Cargo\Content\Options;
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 184.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 12 and the first side effect is on line 35. Open
<?php
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( empty( $_POST ) ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( ! is_admin() ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( is_wp_error( $res ) ) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
// Send options to pusher.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
// Handle error.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$res = cargo()->make( 'pusher' )->send( $data );
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ( is_wp_error( $res ) ) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $res;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return false;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ( ! is_admin() ) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
// Create options content object.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$data = new Options();
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ( empty( $_POST ) ) {
- Exclude checks
Expected 0 spaces after opening bracket; 1 found Open
if ( empty( $_POST ) ) {
- Exclude checks
Space after opening parenthesis of function call prohibited Open
cargo()->action( 'created_term', __NAMESPACE__ . '\\push_options', 999 );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( empty( $_POST ) ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
cargo()->action( 'wp_after_admin_bar_render', __NAMESPACE__ . '\\push_options' );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$res = cargo()->make( 'pusher' )->send( $data );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
cargo()->action( 'save_post', __NAMESPACE__ . '\\push_options', 999 );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
cargo()->action( 'edit_term', __NAMESPACE__ . '\\push_options', 999 );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
cargo()->action( 'created_term', __NAMESPACE__ . '\\push_options', 999 );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
cargo()->action( 'edit_term', __NAMESPACE__ . '\\push_options', 999 );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$res = cargo()->make( 'pusher' )->send( $data );
- Exclude checks
Expected 0 spaces after opening bracket; 1 found Open
if ( is_wp_error( $res ) ) {
- Exclude checks
Space after opening parenthesis of function call prohibited Open
cargo()->action( 'save_post', __NAMESPACE__ . '\\push_options', 999 );
- Exclude checks
Expected 0 spaces after opening bracket; 1 found Open
if ( ! is_admin() ) {
- Exclude checks
Space after opening parenthesis of function call prohibited Open
if ( is_wp_error( $res ) ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( is_wp_error( $res ) ) {
- Exclude checks
Space after opening parenthesis of function call prohibited Open
if ( empty( $_POST ) ) {
- Exclude checks
Opening brace should be on a new line Open
function push_options() {
- Exclude checks
Space after opening parenthesis of function call prohibited Open
cargo()->action( 'wp_after_admin_bar_render', __NAMESPACE__ . '\\push_options' );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$res = cargo()->make( 'pusher' )->send( $data );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$res = cargo()->make( 'pusher' )->send( $data );
- Exclude checks