Skip to content

Commit 701024d

Browse files
committed
remove duplicated code
1 parent b427249 commit 701024d

File tree

1 file changed

+25
-76
lines changed

1 file changed

+25
-76
lines changed

src/Ifsnop/Mysqldump/Mysqldump.php

Lines changed: 25 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,10 +1346,7 @@ public function __construct ($dbHandler)
13461346

13471347
public function databases()
13481348
{
1349-
if (func_num_args() != 1) {
1350-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1351-
}
1352-
1349+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
13531350
$args = func_get_args();
13541351
$databaseName = $args[0];
13551352

@@ -1491,66 +1488,45 @@ public function create_procedure($row, $dumpSettings)
14911488

14921489
public function show_tables()
14931490
{
1494-
if (func_num_args() != 1) {
1495-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1496-
}
1497-
1491+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
14981492
$args = func_get_args();
1499-
15001493
return "SELECT TABLE_NAME AS tbl_name " .
15011494
"FROM INFORMATION_SCHEMA.TABLES " .
15021495
"WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='${args[0]}'";
15031496
}
15041497

15051498
public function show_views()
15061499
{
1507-
if (func_num_args() != 1) {
1508-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1509-
}
1510-
1500+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
15111501
$args = func_get_args();
1512-
15131502
return "SELECT TABLE_NAME AS tbl_name " .
15141503
"FROM INFORMATION_SCHEMA.TABLES " .
15151504
"WHERE TABLE_TYPE='VIEW' AND TABLE_SCHEMA='${args[0]}'";
15161505
}
15171506

15181507
public function show_triggers()
15191508
{
1520-
if (func_num_args() != 1) {
1521-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1522-
}
1523-
1509+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
15241510
$args = func_get_args();
1525-
15261511
return "SHOW TRIGGERS FROM `${args[0]}`;";
15271512
}
15281513

15291514
public function show_columns()
15301515
{
1531-
if (func_num_args() != 1) {
1532-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1533-
}
1534-
1516+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
15351517
$args = func_get_args();
1536-
15371518
return "SHOW COLUMNS FROM `${args[0]}`;";
15381519
}
15391520

15401521
public function show_procedures()
15411522
{
1542-
if (func_num_args() != 1) {
1543-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1544-
}
1545-
1523+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
15461524
$args = func_get_args();
1547-
15481525
return "SELECT SPECIFIC_NAME AS procedure_name " .
15491526
"FROM INFORMATION_SCHEMA.ROUTINES " .
15501527
"WHERE ROUTINE_TYPE='PROCEDURE' AND ROUTINE_SCHEMA='${args[0]}'";
15511528
}
15521529

1553-
15541530
public function setup_transaction()
15551531
{
15561532
return "SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ";
@@ -1568,10 +1544,7 @@ public function commit_transaction()
15681544

15691545
public function lock_table()
15701546
{
1571-
if (func_num_args() != 1) {
1572-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1573-
}
1574-
1547+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
15751548
$args = func_get_args();
15761549
return $this->dbHandler->exec("LOCK TABLES `${args[0]}` READ LOCAL");
15771550

@@ -1584,10 +1557,7 @@ public function unlock_table()
15841557

15851558
public function start_add_lock_table()
15861559
{
1587-
if (func_num_args() != 1) {
1588-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1589-
}
1590-
1560+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
15911561
$args = func_get_args();
15921562

15931563
return "LOCK TABLES `${args[0]}` WRITE;" . PHP_EOL;
@@ -1600,19 +1570,15 @@ public function end_add_lock_table()
16001570

16011571
public function start_add_disable_keys()
16021572
{
1603-
if (func_num_args() != 1) {
1604-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1605-
}
1573+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
16061574
$args = func_get_args();
16071575
return "/*!40000 ALTER TABLE `${args[0]}` DISABLE KEYS */;" .
16081576
PHP_EOL;
16091577
}
16101578

16111579
public function end_add_disable_keys()
16121580
{
1613-
if (func_num_args() != 1) {
1614-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1615-
}
1581+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
16161582
$args = func_get_args();
16171583
return "/*!40000 ALTER TABLE `${args[0]}` ENABLE KEYS */;" .
16181584
PHP_EOL;
@@ -1630,10 +1596,7 @@ public function end_disable_autocommit()
16301596

16311597
public function add_drop_database()
16321598
{
1633-
if (func_num_args() != 1) {
1634-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1635-
}
1636-
1599+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
16371600
$args = func_get_args();
16381601

16391602
return "/*!40000 DROP DATABASE IF EXISTS `${args[0]}`*/;" .
@@ -1642,46 +1605,30 @@ public function add_drop_database()
16421605

16431606
public function add_drop_trigger()
16441607
{
1645-
if (func_num_args() != 1) {
1646-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1647-
}
1648-
1608+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
16491609
$args = func_get_args();
1650-
16511610
return "DROP TRIGGER IF EXISTS `${args[0]}`;" . PHP_EOL;
16521611
}
16531612

16541613
public function drop_table()
16551614
{
1656-
if (func_num_args() != 1) {
1657-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1658-
}
1659-
1615+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
16601616
$args = func_get_args();
1661-
16621617
return "DROP TABLE IF EXISTS `${args[0]}`;" . PHP_EOL;
16631618
}
16641619

16651620
public function drop_view()
16661621
{
1667-
if (func_num_args() != 1) {
1668-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1669-
}
1670-
1622+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
16711623
$args = func_get_args();
1672-
16731624
return "DROP TABLE IF EXISTS `${args[0]}`;" . PHP_EOL .
16741625
"/*!50001 DROP VIEW IF EXISTS `${args[0]}`*/;" . PHP_EOL;
16751626
}
16761627

16771628
public function getDatabaseHeader()
16781629
{
1679-
if (func_num_args() != 1) {
1680-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1681-
}
1682-
1630+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
16831631
$args = func_get_args();
1684-
16851632
return "--" . PHP_EOL .
16861633
"-- Current Database: `${args[0]}`" . PHP_EOL .
16871634
"--" . PHP_EOL . PHP_EOL;
@@ -1717,10 +1664,7 @@ public function parseColumnType($colType)
17171664

17181665
public function backup_parameters()
17191666
{
1720-
if (func_num_args() != 1) {
1721-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1722-
}
1723-
1667+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
17241668
$args = func_get_args();
17251669
$dumpSettings = $args[0];
17261670
$ret = "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;" . PHP_EOL .
@@ -1743,10 +1687,7 @@ public function backup_parameters()
17431687

17441688
public function restore_parameters()
17451689
{
1746-
if (func_num_args() != 1) {
1747-
throw new Exception("Unexpected parameter passed to " . __METHOD__);
1748-
}
1749-
1690+
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
17501691
$args = func_get_args();
17511692
$dumpSettings = $args[0];
17521693
$ret = "";
@@ -1765,4 +1706,12 @@ public function restore_parameters()
17651706

17661707
return $ret;
17671708
}
1709+
1710+
private function check_parameters($num_args, $expected_num_args, $method_name)
1711+
{
1712+
if ( $num_args != $expected_num_args ) {
1713+
throw new Exception("Unexpected parameter passed to $method_name");
1714+
}
1715+
return;
1716+
}
17681717
}

0 commit comments

Comments
 (0)