| Conditions | 7 |
| Paths | 6 |
| Total Lines | 71 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 68 | public function convert( $post_id, &$references = array() ) { |
||
| 69 | |||
| 70 | // Get the post instance. |
||
| 71 | if ( null === $post = get_post( $post_id ) ) { |
||
| 72 | // Post not found. |
||
| 73 | return null; |
||
| 74 | } |
||
| 75 | |||
| 76 | // Get the base JSON-LD and the list of entities referenced by this entity. |
||
| 77 | $jsonld = parent::convert( $post_id, $references ); |
||
| 78 | |||
| 79 | // Get the entity name. |
||
| 80 | $jsonld['headline'] = $post->post_title; |
||
| 81 | |||
| 82 | // Get the author. |
||
| 83 | $author = get_the_author_meta( 'display_name', $post->post_author ); |
||
| 84 | $author_id = $this->user_service->get_uri( $post->post_author ); |
||
| 85 | |||
| 86 | $jsonld['author'] = array( |
||
| 87 | '@type' => 'Person', |
||
| 88 | '@id' => $author_id, |
||
| 89 | 'name' => $author, |
||
| 90 | ); |
||
| 91 | |||
| 92 | // Set the published and modified dates. |
||
| 93 | $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
||
| 94 | $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
||
| 95 | |||
| 96 | // Set the publisher. |
||
| 97 | $this->set_publisher( $jsonld ); |
||
| 98 | |||
| 99 | // Process the references if any. |
||
| 100 | if ( 0 < sizeof( $references ) ) { |
||
| 101 | |||
| 102 | // Prepare the `about` and `mentions` array. |
||
|
|
|||
| 103 | $about = $mentions = array(); |
||
| 104 | |||
| 105 | // If the entity is in the title, then it should be an `about`. |
||
| 106 | foreach ( $references as $reference ) { |
||
| 107 | |||
| 108 | // Get the entity labels. |
||
| 109 | $labels = $this->entity_service->get_labels( $reference ); |
||
| 110 | |||
| 111 | // Get the entity URI. |
||
| 112 | $item = array( '@id' => $this->entity_service->get_uri( $reference ) ); |
||
| 113 | |||
| 114 | // Check if the labels match any part of the title. |
||
| 115 | $matches = 1 === preg_match( '/' . implode( '|', $labels ) . '/', $post->post_title ); |
||
| 116 | |||
| 117 | // If the title matches, assign the entity to the about, otherwise to the mentions. |
||
| 118 | if ( $matches ) { |
||
| 119 | $about[] = $item; |
||
| 120 | } else { |
||
| 121 | $mentions[] = $item; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | // If we have abouts, assign them to the JSON-LD. |
||
| 126 | if ( 0 < sizeof( $about ) ) { |
||
| 127 | $jsonld['about'] = $about; |
||
| 128 | } |
||
| 129 | |||
| 130 | // If we have mentions, assign them to the JSON-LD. |
||
| 131 | if ( 0 < sizeof( $mentions ) ) { |
||
| 132 | $jsonld['mentions'] = $mentions; |
||
| 133 | } |
||
| 134 | |||
| 135 | } |
||
| 136 | |||
| 137 | return $jsonld; |
||
| 138 | } |
||
| 139 | |||
| 205 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.