Completed
Push — master ( b6760c...d0c54a )
by J.D.
03:04
created

WordPoints_Hook_Event_Dynamic   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 12.82 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 3
lcom 1
cbo 3
dl 5
loc 39
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A get_entity_title() 5 18 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Dynamic hook event class.
5
 *
6
 * @package wordpoints-hooks-api
7
 * @since 1.0.0
8
 */
9
10
/**
11
 * Represents a hook event that is attached to a dynamic entity.
12
 *
13
 * Dynamic events, like dynamic entities, have slugs that are prefixed with a generic
14
 * identifier (like 'post'). After this comes a hyphen (-) and then the dynamic part
15
 * of the name.
16
 *
17
 * This class offers a helper method to let dynamic events build better titles and
18
 * descriptions by using a dynamic entity title (like "Page" or "Order"), instead of
19
 * hard-coding something generic (like "Post").
20
 *
21
 * To retrieve the entity title, we just rip off the generic part of the event slug
22
 * and replace it with the generic part of the entity slug. Then we just retrieve
23
 * the entity and return its title.
24
 *
25
 * @since 1.0.0
26
 */
27
abstract class WordPoints_Hook_Event_Dynamic extends WordPoints_Hook_Event {
28
29
	/**
30
	 * The generic portion of the entity slug.
31
	 *
32
	 * @since 1.0.0
33
	 *
34
	 * @var string
35
	 */
36
	protected $generic_entity_slug;
37
38
	/**
39
	 * Get the title of the entity.
40
	 *
41
	 * This is useful to interpolate into your event title and description.
42
	 *
43
	 * @since 1.0.0
44
	 *
45
	 * @return string The title of the entity.
46
	 */
47
	protected function get_entity_title() {
48
49
		$parts = explode( '\\', $this->slug, 2 );
50
51 View Code Duplication
		if ( isset( $parts[1] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
			$entity_slug = "{$this->generic_entity_slug}\\{$parts[1]}";
53
		} else {
54
			$entity_slug = $this->generic_entity_slug;
55
		}
56
57
		$entity = wordpoints_entities()->get( $entity_slug );
58
59
		if ( ! $entity instanceof WordPoints_Entity ) {
60
			return $this->slug;
61
		}
62
63
		return $entity->get_title();
64
	}
65
}
66
67
// EOF
68