Completed
Pull Request — master (#25)
by Matt
02:01
created

mysql_index   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 13.85 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 1
dl 9
loc 65
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A effectively_installed() 0 4 1
A depends_on() 0 8 1
A update_data() 9 9 1
A add_topic_title_fulltext() 0 13 2
A get_driver() 0 4 1

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
 * Precise Similar Topics
5
 *
6
 * @copyright (c) 2018 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\similartopics\migrations\release_1_5_x;
12
13
class mysql_index extends \phpbb\db\migration\migration
14
{
15
	/**
16
	 * Do not run this migration if the DB is not MySQL
17
	 *
18
	 * @return bool
19
	 */
20
	public function effectively_installed()
21
	{
22
		return strpos($this->db->get_sql_layer(), 'mysql') !== 0;
23
	}
24
25
	public static function depends_on()
26
	{
27
		return array(
28
			'\vse\similartopics\migrations\release_1_1_0_data',
29
			'\vse\similartopics\migrations\release_1_3_0_fulltext',
30
			'\vse\similartopics\migrations\release_1_4_3_data',
31
		);
32
	}
33
34 View Code Duplication
	public function update_data()
35
	{
36
		return array(
37
			array('if', array(
38
				strpos($this->db->get_sql_layer(), 'mysql') === 0,
39
				array('custom', array(array($this, 'add_topic_title_fulltext'))),
40
			)),
41
		);
42
	}
43
44
	/**
45
	 * Add a MYSQLI FULLTEXT index to phpbb_topics.topic_title (if needed).
46
	 *
47
	 * This is a retry of the similar action in release_1_1_0_data.
48
	 * The first attempt would miss older InnoDB tables that do not support
49
	 * FULLTEXT. If that happened, then this will convert the table to
50
	 * MyISAM so we can then create the fulltext index. The old storage
51
	 * engine will be stored so it can be reverted on uninstall.
52
	 * Data is reverted in the release_1_3_0_fulltext migration.
53
	 */
54
	public function add_topic_title_fulltext()
55
	{
56
		$driver = $this->get_driver();
57
58
		if (!$driver->is_fulltext('topic_title'))
59
		{
60
			// Store the original database storage engine in a config var for recovery on uninstall
61
			$this->config->set('similar_topics_fulltext', (string) $driver->get_engine());
62
63
			// Create the FULLTEXT index
64
			$driver->create_fulltext_index('topic_title');
65
		}
66
	}
67
68
	/**
69
	 * Get an instance of the similartopics mysqli driver
70
	 *
71
	 * @return \vse\similartopics\driver\mysqli
72
	 */
73
	protected function get_driver()
74
	{
75
		return new \vse\similartopics\driver\mysqli($this->db);
76
	}
77
}
78