Skip to content

Commit

Permalink
- Updated code readability/complexity and cleaned up PEAR CS (thanks
Browse files Browse the repository at this point in the history
  to Gordon Stratton)


git-svn-id: http://svn.php.net/repository/pear/packages/Net_MAC/trunk@254202 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
coreone committed Mar 4, 2008
1 parent 07c5c73 commit bd8ddb7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
56 changes: 29 additions & 27 deletions MAC.php
Expand Up @@ -143,13 +143,14 @@ class Net_MAC
* an error during construction so the constructor should be
* called from a try/catch block
*/
function __construct(&$db, $options = NULL) {
function __construct(&$db, $options = NULL)
{
require_once 'MDB2.php';

$this->_db = NULL;
$this->_macaddr = NULL;
$this->_dbOptions = NULL;

if (!$db instanceof MDB2_Driver_Common) {
throw new Net_MAC_Exception('Bad database object', NET_MAC_ERROR_BADDB);
}
Expand Down Expand Up @@ -197,8 +198,8 @@ function __construct(&$db, $options = NULL) {
static function check($input, $delimiter = ':')
{
// Check for 6 octets without any punctuation
$retval = preg_match('/^([0-9a-fA-F][0-9a-fA-F]\Q'.$delimiter.'\E){5}([0-9a-fA-F][0-9a-fA-F]){1}$/', $input);
$retval = preg_match('/^([[:xdigit:]]{2}'.$delimiter.'){5}[[:xdigit:]]{2}$/', $input);

return $retval;
} /* end method check */

Expand Down Expand Up @@ -255,24 +256,28 @@ static function format($input, $delimiter = ':', $uppercase = true)
* nothing. We are going to be testing for a MAC address as
* XXXXXXXXXXXX instead of XX:XX:XX:XX:XX:XX
*/
$macaddr = preg_replace('/[g-zG-Z]*\W*_*/', '', $input);
$macaddr = preg_replace('/[^[:xdigit:]]/', '', $input);

/* If $uppercase is true, set all the alpha characters to
* uppercase, otherwise set all the alpha characters to
* lowercase
* lowercase
*/
$macaddr = ($uppercase) ? strtoupper($macaddr) : strtolower($macaddr);

// Check for 6 octets without any punctuation
if (!preg_match('/^([0-9a-fA-F][0-9a-fA-F]){6}$/', $macaddr)) {
/*
* Our character replacement guarantees we have no non-hex digits, so
* the last thing we have to do is verify the appropriate length for
* six hex octets.
*/
if (strlen($macaddr) !== 12) {
return false;
}

// Add back in the $delimiter delimiters
$macaddr = preg_replace('/([0-9a-fA-F][0-9a-fA-F]){1}/', '$1' . $delimiter, $macaddr);

// Remove the trailing $delimiter
$macaddr = preg_replace('/' . $delimiter . '$/', '', $macaddr);
/*
* Add back in the $delimiter delimiters without the unwanted final
* delimiter.
*/
$macaddr = substr(chunk_split($macaddr, 2, $delimiter), 0, -1);

return $macaddr;
} /* end method format */
Expand Down Expand Up @@ -304,7 +309,8 @@ static function format($input, $delimiter = ':', $uppercase = true)
* success. A {@link Net_MAC_Exception Net_MAC_Exception}
* Exception object will be thrown on failure in either case.
*/
function importVendors($file, $doReturn = false) {
function importVendors($file, $doReturn = false)
{
if ($file == NULL) {
throw new Net_MAC_Exception('No file or URL given', NET_MAC_ERROR_BADFILE);
}
Expand All @@ -316,8 +322,7 @@ function importVendors($file, $doReturn = false) {

if ($doReturn) {
$retArr = array();
}
else {
} else {
// Prepare parameters for MDB2->buildManipSQL()
$fields = array ($this->_dbOptions['macaddrcol'],
$this->_dbOptions['vendorcol'],
Expand All @@ -331,11 +336,11 @@ function importVendors($file, $doReturn = false) {

while ($line = fgets($fp, NET_MAC_LINE_MAXLENGTH)) {
// Remove comments
if ( preg_match('/^\#/', $line) ) {
if (preg_match('/^\#/', $line)) {
continue;
}

if ( preg_match('/^([0-9A-Fa-f][0-9A-Fa-f]:){2}([0-9A-Fa-f][0-9A-Fa-f]){1}/', $line) ) {
if (preg_match('/^([[:xdigit:]]{2}:){2}[[:xdigit:]]{2}/', $line)) {
$pieces = preg_split('/\s+/', $line);

// Since the file is space-delimited, we now need to
Expand All @@ -346,12 +351,11 @@ function importVendors($file, $doReturn = false) {
}
$desc = rtrim($desc);

if ( (isset($pieces[0])) && (isset($pieces[1])) ) {
if ((isset($pieces[0])) && (isset($pieces[1]))) {
if ($doReturn) {
$retArr[$pieces[0]] = array('vendor' => $pieces[1],
'description' => $desc);
}
else {
} else {
$values = array($pieces[0], $pieces[1], $desc);
$result = $query->execute($values);
}
Expand Down Expand Up @@ -411,7 +415,7 @@ function findVendor($getDescription = false, $macList = NULL)
$query = $this->_db->prepare($sql);
$result = $query->execute();

if ( (MDB2::isError($result)) || ($result->numRows() == 0) ) {
if ((MDB2::isError($result)) || ($result->numRows() == 0)) {
return false;
}

Expand All @@ -420,12 +424,10 @@ function findVendor($getDescription = false, $macList = NULL)
if ($getDescription) {
return array($vendorcol => $macInfo[$vendorcol],
$desccol => $macInfo[$desccol]);
}
else {
} else {
return $macInfo[$vendorcol];
}
}
else {
} else {
return false;
}
} /* end method findVendor */
Expand Down
24 changes: 19 additions & 5 deletions package2.xml
Expand Up @@ -15,19 +15,18 @@ http://pear.php.net/dtd/package-2.0.xsd">
<email>atex@php.net</email>
<active>yes</active>
</lead>
<date>2007-05-01</date>
<date>2008-03-04</date>
<version>
<release>0.1.4</release>
<release>0.1.5</release>
<api>0.1.0</api>
</version>
<stability>
<release>alpha</release>
<api>alpha</api>
</stability>
<license uri="http://www.opensource.org/licenses/bsd-license.php">BSD License</license>
<notes>* Updated the license to BSD (per Justin Patrin)
* Updated requires to require PHP 5.0 instead of 4.2 (per Mark Wiesemann)
* Updated some PEAR CS
<notes>* Updates to PEAR CS (thanks to Gordon Stratton)
* Updates to code readability (thanks to Gordon Stratton)
</notes>
<contents>
<dir name="/">
Expand All @@ -48,6 +47,21 @@ http://pear.php.net/dtd/package-2.0.xsd">
</dependencies>
<phprelease />
<changelog>
<release>
<version>
<release>0.1.5</release>
<api>0.1.0</api>
</version>
<stability>
<release>alpha</release>
<api>alpha</api>
</stability>
<date>2008-03-04</date>
<license uri="http://www.opensource.org/licenses/bsd-license.php">BSD License</license>
<notes>* Updates to PEAR CS (thanks to Gordon Stratton)
* Updates to code readability (thanks to Gordon Stratton)
</notes>
</release>
<release>
<version>
<release>0.1.4</release>
Expand Down

0 comments on commit bd8ddb7

Please sign in to comment.