Php 重构技巧
我在保理业务方面没有什么经验。我的代码很长,我不使用函数,因为我不知道它是否需要是函数。我希望你能给我一些提示,这样我就可以清理我的代码了Php 重构技巧,php,mysql,refactoring,Php,Mysql,Refactoring,我在保理业务方面没有什么经验。我的代码很长,我不使用函数,因为我不知道它是否需要是函数。我希望你能给我一些提示,这样我就可以清理我的代码了 <?php # Required files include("simple-html-dom.php"); require("{$_SERVER['DOCUMENT_ROOT']}/config/pipeline-x.php"); # Define variables $fn = urlencode($_REQUEST['fn']); $ln =
<?php
# Required files
include("simple-html-dom.php");
require("{$_SERVER['DOCUMENT_ROOT']}/config/pipeline-x.php");
# Define variables
$fn = urlencode($_REQUEST['fn']);
$ln = urlencode($_REQUEST['ln']);
# Connect to database
$db = new px_dbasei();
$db->connect("192.168.50.70", "****", "****", "piasdgeline_tesh45t");
# Query database if a record exist
$sql = "SELECT * FROM linkedin_parse "
."WHERE "
."`first_name` = '{$fn}' AND "
."`last_name` = '{$ln}' ";
$results = $db->query($sql);
# If there is no result
if($results->num_rows == 0):
# Search linkedin and download page
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.linkedin.com/pub/dir/?first={$fn}&last={$ln}&search=Search");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
$res = curl_exec($ch);
curl_close($ch);
$html = str_get_html($res);
# Parse records from the download page
foreach($html->find('li.vcard') as $vcard):
$table = array();
foreach($vcard->find('span.given-name') as $given_name):
$table['first_name'] = (trim(addslashes($given_name->plaintext), " "));
endforeach;
foreach($vcard->find('span.family-name') as $family_name):
$table['last_name'] = (trim(addslashes($family_name->plaintext)," "));
endforeach;
foreach($vcard->find('span.location') as $location):
$table['location'] = (trim(addslashes($location->plaintext), " "));
endforeach;
foreach($vcard->find('span.industry') as $industry):
$table['industry'] = (trim(addslashes($industry->plaintext), " "));
endforeach;
foreach($vcard->find('dd.current-content') as $headline):
$table['headline'] = (trim(addslashes($headline->plaintext), " "));
endforeach;
foreach($vcard->find('a.btn-primary') as $url):
$table['url'] = addslashes($url->href);
endforeach;
# Insert generated results to the database
$sql = "INSERT INTO linkedin_parse (`first_name`,`last_name`,`location`,`industry`,`headline`,`url`) "
."VALUES "
."('{$table['first_name']}',"
."'{$table['last_name']}',"
."'{$table['location']}',"
."'{$table['industry']}',"
."'{$table['headline']}',"
."'{$table['url']}')";
$db->query($sql);
# Get last insert id and query database again
$new_id = $db->insert_id();
$sql2 = "SELECT * FROM linkedin_parse WHERE `linkedin_parse_id` = '{$new_id}'";
$result = $db->query($sql2);
# Display results in HTML
?>
<ol>
<?php while($row = $result->fetch_assoc()): ?>
<li class="vcard">
<span class="given-name"><?php echo $row['first_name'] ?></span>
<span class="family-name"><?php echo $row['last_name'] ?></span>
<span class="location"><?php echo $row['location'] ?></span>
<span class="industry"><?php echo $row['industry'] ?></span>
<dd class="current-content">
<span><?php echo $row['headline'] ?></span>
</dd>
<a href="<?php echo $row['url'] ?>"></a>
</li>
<?php endwhile; ?>
</ol>
<?php
endforeach;
else:
# Query database if record is 30 days old
$sql = "SELECT * FROM linkedin_parse "
."WHERE "
."`first_name` = '{$fn}' AND"
."`last_name` = '{$ln}' AND"
."`date_inserted` >= DATE_SUB(NOW(), INTERVAL 30 DAY)";
$results = $db->query($sql);
if($results->num_rows != 0):
# Retrieve from database
$sql = "SELECT * FROM linkedin_parse "
."WHERE "
."`first_name` = '{$fn}' AND"
."`last_name` = '{$ln}' ";
$result = $db->query($sql);
# Display results in HTML
?>
<ol>
<?php while($row = $result->fetch_assoc()): ?>
<li class="vcard">
<span class="given-name"><?php echo $row['first_name'] ?></span>
<span class="family-name"><?php echo $row['last_name'] ?></span>
<span class="location"><?php echo $row['location'] ?></span>
<span class="industry"><?php echo $row['industry'] ?></span>
<dd class="current-content">
<span><?php echo $row['headline'] ?></span>
</dd>
<a href="<?php echo $row['url'] ?>"></a>
</li>
<?php endwhile; ?>
</ol>
<?php
else:
# Search linked-in for updated records
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.linkedin.com/pub/dir/?first={$fn}&last={$ln}&search=Search");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
$res = curl_exec($ch);
curl_close($ch);
$html = str_get_html($res);
# Parse records from the download page
foreach($html->find('li.vcard') as $vcard):
$table = array();
foreach($vcard->find('span.given-name') as $given_name):
$table['first_name'] = (trim(addslashes($given_name->plaintext), " "));
endforeach;
foreach($vcard->find('span.family-name') as $family_name):
$table['last_name'] = (trim(addslashes($family_name->plaintext)," "));
endforeach;
foreach($vcard->find('span.location') as $location):
$table['location'] = (trim(addslashes($location->plaintext), " "));
endforeach;
foreach($vcard->find('span.industry') as $industry):
$table['industry'] = (trim(addslashes($industry->plaintext), " "));
endforeach;
foreach($vcard->find('dd.current-content') as $headline):
$table['headline'] = (trim(addslashes($headline->plaintext), " "));
endforeach;
foreach($vcard->find('a.btn-primary') as $url):
$table['url'] = addslashes($url->href);
endforeach;
# Update records
$sql = "UPDATE linkedin_parse "
."SET "
."`date_inserted` = now(),"
."`first_name` = '{$table['first_name']}',"
."`last_name` = '{$table['last_name']}', "
."`location` = '{$table['location']}', "
."`industry` = '{$table['industry']}', "
."`headline` = '{$table['headline']}', "
."`url` = '{$table['url']}' "
."WHERE "
."`first_name` = '{$table['first_name']}' AND"
."`last_name` = '{$table['last_name']}' AND "
."`location` = '{$table['location']}' ";
$result = $db->query($sql);
?>
<ol>
<?php while($row = $result->fetch_assoc()): ?>
<li class="vcard">
<span class="given-name"><?php echo $row['given-name'] ?></span>
<span class="family-name"><?php echo $row['family-name'] ?></span>
<span class="location"><?php echo $row['location'] ?></span>
<span class="industry"><?php echo $row['industry'] ?></span>
<dd class="current-content">
<span><?php echo $row['headline'] ?></span>
</dd>
<a href="<?php echo $row['url'] ?>"></a>
</li>
<?php endwhile; ?>
</ol>
<?php
endforeach;
endif;
endif;
作为一个一般概念,我推荐几件事:
- 正如其他人所提到的,“干燥”或“不要重复你自己”是一个很好的概念。如果你不止一次地做某件事,很可能它应该有自己的功能,或者可以简化
- 虽然“单一责任原则”通常适用于面向对象编程,但由于函数的可维护性,“单一责任原则”可以很好地应用于函数,但也应该避免仅仅因为可以创建函数而创建函数(函数调用需要开销)。最终,函数的集合通常以可重用类结束
- “接吻”或“保持简单,愚蠢”(注:这最好被视为一个自我引用的“愚蠢”,比如当有人说,“我真是个白痴!”当他们发现某件事时——尽可能简化逻辑和代码。“最简单的解释通常是正确的。”
为了应用这些概念,以下是我将如何重新构造(而不是如何编写)您的脚本:
查询是否有少于30天的配置文件匹配,因为您正在更新没有配置文件或所有配置文件都超过30天的配置文件
如果未返回任何行:
- 查询所有配置文件是否匹配,以确定更新与插入
- 下载并解析页面
- 保存新的/更新的记录
- 保留已解析的匹配项,而不是从数据库下载刚插入/更新的内容
最后,显示匹配项(不管它们是来自数据库还是来自解析)
通过以下方式重新构造代码:
- 您消除了大部分重复和潜在的混淆
- 您可以简化代码路径
- 您可以对逻辑组件进行分组(搜索、解析/存储(如有必要)、显示)
- 所有HTML都可以放在脚本的末尾,这样可读性更强(或者可以轻松地放在单独的文件中)
- 函数有意义的地方将更加明显,例如解析循环
最后一点注意——无论何时处理来自“未知源”(用户、网站、提供的文件等)的数据,都应该强调安全性。虽然addslashes()
和urlencode()
是一个好主意,但有许多资源可以帮助您了解如何避免SQL注入、跨站点脚本编写和其他潜在威胁。代码中的一个风险示例是在不转义数据库查询的情况下使用$\u请求 有人说:“如果你有三个以上的缩进级别,就把它重构成一个函数。”你只是发布了你的数据库凭证。此外,您的代码绝不是非精确安全的……这属于您自己,您将对SQL注入敞开大门。请了解如何使用参数化查询(最好是PDO模块)来保护您的web应用程序。有一些例子可以让你开始。哦,不,我让它看起来像吉伯里语。但是谢谢你加密。我们公司有一个框架,它使用mysqli,所以我需要使用它。但我会在这里阅读您的所有评论,谢谢。回答得很详细。我会实现这一点,稍后发布我的更新代码:)虽然有些我还不知道怎么做,但我会重构我的代码。=)谢谢