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

postgres_index   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 11.69 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
lcom 0
cbo 1
dl 9
loc 77
rs 10
c 3
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A depends_on() 0 4 1
A revert_data() 9 9 1
A effectively_installed() 0 4 2
A update_data() 0 11 1
A create_postgres_index() 0 5 1
A drop_postgres_changes() 0 10 2
A get_driver() 0 4 1
A get_ts_name() 0 4 2

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 postgres_index extends \phpbb\db\migration\migration
14
{
15
	/**
16
	 * Do not run this migration if the DB is not PostgreSQL
17
	 *
18
	 * @return bool
19
	 */
20
	public function effectively_installed()
21
	{
22
		return $this->db->get_sql_layer() !== 'postgres' || $this->config->offsetExists('pst_postgres_ts_name');
23
	}
24
25
	public static function depends_on()
26
	{
27
		return array('\vse\similartopics\migrations\release_1_4_3_data');
28
	}
29
30
	public function update_data()
31
	{
32
		return array(
33
			array('config.update', array('similar_topics_sense', 1)),
34
			array('config.add', array('pst_postgres_ts_name', $this->get_ts_name())),
35
			array('if', array(
36
				$this->db->get_sql_layer() === 'postgres',
37
				array('custom', array(array($this, 'create_postgres_index'))),
38
			)),
39
		);
40
	}
41
42 View Code Duplication
	public function revert_data()
43
	{
44
		return array(
45
			array('if', array(
46
				$this->db->get_sql_layer() === 'postgres',
47
				array('custom', array(array($this, 'drop_postgres_changes'))),
48
			)),
49
		);
50
	}
51
52
	/**
53
	 * Create PostgreSQL FULLTEXT index for the topic_title
54
	 */
55
	public function create_postgres_index()
56
	{
57
		$driver = $this->get_driver();
58
		$driver->create_fulltext_index();
59
	}
60
61
	/**
62
	 * Drop the PostgreSQL FULLTEXT index on phpbb_topics.topic_title
63
	 */
64
	public function drop_postgres_changes()
65
	{
66
		$driver = $this->get_driver();
67
68
		foreach ($driver->get_fulltext_indexes() as $index)
69
		{
70
			$sql = 'DROP INDEX ' . $index;
71
			$this->db->sql_query($sql);
72
		}
73
	}
74
75
	/**
76
	 * Get an instance of the similartopics POSTGRES driver
77
	 *
78
	 * @return \vse\similartopics\driver\postgres
79
	 */
80
	protected function get_driver()
81
	{
82
		return new \vse\similartopics\driver\postgres($this->db, $this->config);
83
	}
84
85
	protected function get_ts_name()
86
	{
87
		return $this->config->offsetExists('fulltext_postgres_ts_name') ? $this->config['fulltext_postgres_ts_name'] : 'simple';
88
	}
89
}
90