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 |
||
23 | class RevisionGetter { |
||
24 | |||
25 | protected MediawikiApi $api; |
||
|
|||
26 | |||
27 | protected \Deserializers\Deserializer $entityDeserializer; |
||
28 | |||
29 | /** |
||
30 | * @param MediawikiApi $api |
||
31 | * @param Deserializer $entityDeserializer |
||
32 | */ |
||
33 | public function __construct( MediawikiApi $api, Deserializer $entityDeserializer ) { |
||
34 | $this->api = $api; |
||
35 | $this->entityDeserializer = $entityDeserializer; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @since 0.1 |
||
40 | * @param string|EntityId $id |
||
41 | */ |
||
42 | public function getFromId( $id ): Revision { |
||
43 | if ( $id instanceof EntityId ) { |
||
44 | $id = $id->getSerialization(); |
||
45 | } |
||
46 | |||
47 | $result = $this->api->getRequest( new SimpleRequest( 'wbgetentities', [ 'ids' => $id ] ) ); |
||
48 | return $this->newRevisionFromResult( array_shift( $result['entities'] ) ); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @since 0.1 |
||
53 | * @param SiteLink $siteLink |
||
54 | */ |
||
55 | public function getFromSiteLink( SiteLink $siteLink ): Revision { |
||
56 | $result = $this->api->getRequest( new SimpleRequest( |
||
57 | 'wbgetentities', |
||
58 | [ 'sites' => $siteLink->getSiteId(), 'titles' => $siteLink->getPageName() ] |
||
59 | ) ); |
||
60 | return $this->newRevisionFromResult( array_shift( $result['entities'] ) ); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @since 0.1 |
||
65 | */ |
||
66 | public function getFromSiteAndTitle( string $siteId, string $title ): Revision { |
||
67 | $result = $this->api->getRequest( new SimpleRequest( |
||
68 | 'wbgetentities', |
||
69 | [ 'sites' => $siteId, 'titles' => $title ] |
||
70 | ) ); |
||
71 | return $this->newRevisionFromResult( array_shift( $result['entities'] ) ); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param array $entityResult |
||
76 | * @return bool|Revision |
||
77 | * @todo this could be factored into a different class? |
||
78 | */ |
||
79 | private function newRevisionFromResult( array $entityResult ) { |
||
80 | if ( array_key_exists( 'missing', $entityResult ) ) { |
||
81 | return false; // Throw an exception? |
||
82 | } |
||
83 | return new Revision( |
||
84 | $this->getContentFromEntity( $this->entityDeserializer->deserialize( $entityResult ) ), |
||
85 | new PageIdentifier( null, (int)$entityResult['pageid'] ), |
||
86 | $entityResult['lastrevid'], |
||
87 | null, |
||
88 | null, |
||
89 | $entityResult['modified'] |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param Item|Property $entity |
||
95 | * |
||
96 | * @throws RuntimeException |
||
97 | * @return ItemContent|PropertyContent|void |
||
98 | * @todo this could be factored into a different class? |
||
99 | */ |
||
100 | private function getContentFromEntity( $entity ) { |
||
101 | switch ( $entity->getType() ) { |
||
102 | case Item::ENTITY_TYPE: |
||
103 | return new ItemContent( $entity ); |
||
104 | case Property::ENTITY_TYPE: |
||
105 | return new PropertyContent( $entity ); |
||
106 | default: |
||
107 | throw new RuntimeException( 'I cant get a content for this type of entity' ); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | } |
||
112 |