| Conditions | 4 |
| Paths | 6 |
| Total Lines | 57 |
| 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 |
||
| 146 | public function build_uri( $title, $post_type, $schema_type = null, $increment_digit = 0 ) { |
||
| 147 | |||
| 148 | // Get the entity slug suffix digit |
||
| 149 | $suffix_digit = $increment_digit + 1; |
||
| 150 | |||
| 151 | // Get a sanitized uri for a given title. |
||
| 152 | /* |
||
| 153 | * The call takes into consideration URL encoding. |
||
| 154 | * |
||
| 155 | * @see https://github.com/insideout10/wordlift-plugin/issues/885 |
||
| 156 | * |
||
| 157 | * @since 3.20.0 |
||
| 158 | */ |
||
| 159 | $entity_slug = urldecode( wl_sanitize_uri_path( $title ) ) |
||
|
|
|||
| 160 | . ( 0 === $increment_digit ? '' : '_' . $suffix_digit ); |
||
| 161 | |||
| 162 | // Compose a candidate uri. |
||
| 163 | $new_entity_uri = sprintf( '%s/%s/%s', |
||
| 164 | wl_configuration_get_redlink_dataset_uri(), |
||
| 165 | $post_type, |
||
| 166 | $entity_slug |
||
| 167 | ); |
||
| 168 | |||
| 169 | $this->log->trace( "Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]" ); |
||
| 170 | |||
| 171 | global $wpdb; |
||
| 172 | |||
| 173 | // Check if the candidated uri already is used |
||
| 174 | $stmt = $wpdb->prepare( |
||
| 175 | "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s LIMIT 1", |
||
| 176 | WL_ENTITY_URL_META_NAME, |
||
| 177 | $new_entity_uri |
||
| 178 | ); |
||
| 179 | |||
| 180 | // Perform the query |
||
| 181 | $post_id = $wpdb->get_var( $stmt ); |
||
| 182 | |||
| 183 | // If the post does not exist, then the new uri is returned |
||
| 184 | if ( ! is_numeric( $post_id ) ) { |
||
| 185 | $this->log->trace( "Going to return uri [ new_entity_uri :: $new_entity_uri ]" ); |
||
| 186 | |||
| 187 | return $new_entity_uri; |
||
| 188 | } |
||
| 189 | |||
| 190 | // If schema_type is equal to schema org type of post x, then the new uri is returned |
||
| 191 | $schema_post_type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
||
| 192 | |||
| 193 | // @todo: we shouldn't rely on css classes to take such decisions. |
||
| 194 | if ( $schema_type === $schema_post_type['css_class'] ) { |
||
| 195 | $this->log->trace( "An entity with the same title and type already exists! Return uri [ new_entity_uri :: $new_entity_uri ]" ); |
||
| 196 | |||
| 197 | return $new_entity_uri; |
||
| 198 | } |
||
| 199 | |||
| 200 | // Otherwise the same function is called recursively |
||
| 201 | return $this->build_uri( $title, $post_type, $schema_type, ++ $increment_digit ); |
||
| 202 | } |
||
| 203 | |||
| 205 |
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.