Function run_queue
has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring. Open
public function run_queue() {
$database = $this->cargo->make( 'database' );
$pusher = $this->cargo->make( 'pusher' );
foreach ( $database->all() as $item ) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Refactor this function to reduce its Cognitive Complexity from 19 to the 15 allowed. Open
public function run_queue() {
- Read upRead up
- Exclude checks
Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.
See
The method run_queue uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$this->log_info( sprintf( 'Pushed item with id: %d', $item->id ) );
}
- Read upRead up
- Exclude checks
ElseExpression
Since: 1.4.0
An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.
Example
class Foo
{
public function bar($flag)
{
if ($flag) {
// one branch
} else {
// another branch
}
}
}
Source https://phpmd.org/rules/cleancode.html#elseexpression
The method run_queue uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$this->log_info( sprintf( 'Item with id %d is not pushed since data string is empty', $item->id ) );
}
- Read upRead up
- Exclude checks
ElseExpression
Since: 1.4.0
An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.
Example
class Foo
{
public function bar($flag)
{
if ($flag) {
// one branch
} else {
// another branch
}
}
}
Source https://phpmd.org/rules/cleancode.html#elseexpression
Avoid assigning values to variables in if clauses and the like (line '51', column '10'). Open
public function run_queue() {
$database = $this->cargo->make( 'database' );
$pusher = $this->cargo->make( 'pusher' );
foreach ( $database->all() as $item ) {
- Read upRead up
- Exclude checks
IfStatementAssignment
Since: 2.7.0
Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.
Example
class Foo
{
public function bar($flag)
{
if ($foo = 'bar') { // possible typo
// ...
}
if ($baz = 0) { // always false
// ...
}
}
}
Source http://phpmd.org/rules/cleancode.html#ifstatementassignment
Method name "Basic::run_all" is not in camel caps format Open
public function run_all() {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
foreach ( $this->terms() as $term ) {
- Exclude checks
Space found before closing bracket of FOREACH loop Open
foreach ( $this->posts() as $post ) {
- Exclude checks
Space found before closing bracket of FOREACH loop Open
foreach ( $this->terms() as $term ) {
- Exclude checks
Space found before closing bracket of FOREACH loop Open
foreach ( $database->all() as $item ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
foreach ( $this->posts() as $post ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
foreach ( $database->all() as $item ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( ! empty( $item->data ) && cargo_is_json( $item->data ) ) {
- Exclude checks
Opening brace of a class must be on the line after the definition Open
class Basic extends Abstract_Runner {
- Exclude checks
Space found after opening bracket of FOREACH loop Open
foreach ( $this->terms() as $term ) {
- Exclude checks
Method name "Basic::run_queue" is not in camel caps format Open
public function run_queue() {
- Exclude checks
Space found after opening bracket of FOREACH loop Open
foreach ( $this->posts() as $post ) {
- Exclude checks
Space found after opening bracket of FOREACH loop Open
foreach ( $database->all() as $item ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( ! $database->delete( $item->id ) ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( is_wp_error( $res ) ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( $res = $pusher->send( $item->data ) ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( ! $res ) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Run all content task.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$pusher->send( 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
// Push all terms.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ( $res = $pusher->send( $item->data ) ) {
- 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
$this->log_info( sprintf( 'Pushed post with id %d to pusher', $post->ID ) );
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->log_info( 'Pushed all options' );
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ( ! empty( $item->data ) && cargo_is_json( $item->data ) ) {
- 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
public function run_all() {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ( $this->posts() as $post ) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ( $database->all() as $item ) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$pusher->send( new Menus() );
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$database = $this->cargo->make( 'database' );
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->log_success( 'Done!' );
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$pusher = $this->cargo->make( 'pusher' );
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
// Push all menus.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->log_info( 'Pushed all menus' );
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ( $this->terms() as $term ) {
- 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
// Bail if we don't get a true when we run through all items.
- 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
// Push all posts.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$pusher->send( new Post( $post ) );
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$pusher->send( new Term( $term ) );
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
// Push all options.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
* Run queued content task.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function run_queue() {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$pusher = $this->cargo->make( 'pusher' );
- 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
$this->log_info( sprintf( 'Pushed term with id %d to pusher', $term->term_id ) );
- 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
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->log_info( sprintf( 'Pushed item with id: %d', $item->id ) );
- 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
$this->log_info( sprintf( 'Item with id %d is not pushed since data string is empty', $item->id ) );
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->log_error( $res );
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
// If it can be send delete it from the database.
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ( ! $res ) {
- 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
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->log_success( 'Done!' );
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} else {
- 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
}
- 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
if ( ! $database->delete( $item->id ) ) {
- 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
} else {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
break;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->log_error( sprintf( 'Failed to delete item with id: %d', $item->id ) );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$pusher->send( new Options() );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$pusher = $this->cargo->make( 'pusher' );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$pusher = $this->cargo->make( 'pusher' );
- Exclude checks
Opening brace should be on a new line Open
public function run_all() {
- Exclude checks
Space after opening parenthesis of function call prohibited Open
if ( ! empty( $item->data ) && cargo_is_json( $item->data ) ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$pusher = $this->cargo->make( 'pusher' );
- Exclude checks
Expected 0 spaces after opening bracket; 1 found Open
foreach ( $this->terms() as $term ) {
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$pusher->send( new Term( $term ) );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$this->log_info( sprintf( 'Pushed term with id %d to pusher', $term->term_id ) );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
if ( $res = $pusher->send( $item->data ) ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$pusher->send( new Term( $term ) );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_info( 'Pushed all options' );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
if ( ! empty( $item->data ) && cargo_is_json( $item->data ) ) {
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$pusher->send( new Post( $post ) );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_info( sprintf( 'Pushed post with id %d to pusher', $post->ID ) );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_info( sprintf( 'Pushed post with id %d to pusher', $post->ID ) );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$pusher->send( new Term( $term ) );
- Exclude checks
Opening brace should be on a new line Open
public function run_queue() {
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_info( 'Pushed all menus' );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$pusher->send( new Options() );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$this->log_info( 'Pushed all options' );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_success( 'Done!' );
- Exclude checks
Expected 0 spaces after opening bracket; 1 found Open
foreach ( $database->all() as $item ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$pusher->send( new Post( $post ) );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$this->log_info( sprintf( 'Pushed term with id %d to pusher', $term->term_id ) );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$pusher->send( new Term( $term ) );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$pusher->send( new Menus() );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$this->log_info( 'Pushed all menus' );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( ! empty( $item->data ) && cargo_is_json( $item->data ) ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$this->log_info( sprintf( 'Pushed post with id %d to pusher', $post->ID ) );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_info( sprintf( 'Pushed term with id %d to pusher', $term->term_id ) );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$database = $this->cargo->make( 'database' );
- Exclude checks
Expected 0 spaces after opening bracket; 1 found Open
foreach ( $this->posts() as $post ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$pusher->send( new Menus() );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( ! empty( $item->data ) && cargo_is_json( $item->data ) ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$pusher->send( new Post( $post ) );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$database = $this->cargo->make( 'database' );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$pusher = $this->cargo->make( 'pusher' );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$pusher->send( new Post( $post ) );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_info( sprintf( 'Pushed term with id %d to pusher', $term->term_id ) );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$this->log_success( 'Done!' );
- Exclude checks
Expected 0 spaces after opening bracket; 1 found Open
if ( ! empty( $item->data ) && cargo_is_json( $item->data ) ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$this->log_info( sprintf( 'Pushed post with id %d to pusher', $post->ID ) );
- Exclude checks
Expected 0 spaces after opening bracket; 1 found Open
if ( $res = $pusher->send( $item->data ) ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$this->log_info( sprintf( 'Item with id %d is not pushed since data string is empty', $item->id ) );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_error( sprintf( 'Failed to delete item with id: %d', $item->id ) );
- 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
$this->log_error( sprintf( 'Failed to delete item with id: %d', $item->id ) );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$this->log_error( $res );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
if ( ! $database->delete( $item->id ) ) {
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_success( 'Done!' );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_error( sprintf( 'Failed to delete item with id: %d', $item->id ) );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_info( sprintf( 'Pushed item with id: %d', $item->id ) );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_info( sprintf( 'Item with id %d is not pushed since data string is empty', $item->id ) );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( ! $database->delete( $item->id ) ) {
- Exclude checks
Expected 0 spaces after opening bracket; 1 found Open
if ( ! $res ) {
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_info( sprintf( 'Item with id %d is not pushed since data string is empty', $item->id ) );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$this->log_error( sprintf( 'Failed to delete item with id: %d', $item->id ) );
- Exclude checks
Expected 0 spaces after opening bracket; 1 found Open
if ( is_wp_error( $res ) ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$this->log_info( sprintf( 'Pushed item with id: %d', $item->id ) );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$this->log_success( 'Done!' );
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_info( sprintf( 'Pushed item with id: %d', $item->id ) );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$this->log_info( sprintf( 'Item with id %d is not pushed since data string is empty', $item->id ) );
- Exclude checks
Expected 0 spaces after opening bracket; 1 found Open
if ( ! $database->delete( $item->id ) ) {
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( $res = $pusher->send( $item->data ) ) {
- Exclude checks
Space after opening parenthesis of function call prohibited Open
$this->log_error( $res );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
$this->log_info( sprintf( 'Pushed item with id: %d', $item->id ) );
- Exclude checks
Expected 0 spaces before closing bracket; 1 found Open
if ( is_wp_error( $res ) ) {
- Exclude checks
The method run_all is not named in camelCase. Open
public function run_all() {
$pusher = $this->cargo->make( 'pusher' );
// Push all posts.
foreach ( $this->posts() as $post ) {
- Read upRead up
- Exclude checks
CamelCaseMethodName
Since: 0.2
It is considered best practice to use the camelCase notation to name methods.
Example
class ClassName {
public function get_name() {
}
}
Source
The method run_queue is not named in camelCase. Open
public function run_queue() {
$database = $this->cargo->make( 'database' );
$pusher = $this->cargo->make( 'pusher' );
foreach ( $database->all() as $item ) {
- Read upRead up
- Exclude checks
CamelCaseMethodName
Since: 0.2
It is considered best practice to use the camelCase notation to name methods.
Example
class ClassName {
public function get_name() {
}
}