Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from appstract/analysis-8nLOd1
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
ovanschie authored Apr 18, 2017
2 parents c20cb38 + b9747c4 commit bf7eebd
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/HostsFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ public function __construct($filePath = null)
}
}

if (!is_file($filePath) || !is_readable($filePath)) {
if (! is_file($filePath) || ! is_readable($filePath)) {
throw new Exception(sprintf('Unable to read file: %s', $filePath));
}

$this->filePath = realpath($filePath);
$this->bakPath = realpath($filePath) . '.bak';
$this->bakPath = realpath($filePath).'.bak';

$this->readFile();
}

/**
* Return lines
* Return lines.
*
* @return array
*/
Expand All @@ -59,7 +59,7 @@ public function getLines()
}

/**
* Add a line
* Add a line.
*
* @param $ip
* @param $domain
Expand All @@ -70,11 +70,11 @@ public function getLines()
*/
public function addLine($ip, $domain, $aliases = '')
{
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
if (! filter_var($ip, FILTER_VALIDATE_IP)) {
throw new Exception(sprintf("'%s', is not a valid ip", $ip));
}

if (!filter_var($domain, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => "/^[a-zA-Z0-9\\.]*[a-zA-Z0-9]+?/"]])) {
if (! filter_var($domain, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^[a-zA-Z0-9\\.]*[a-zA-Z0-9]+?/']])) {
throw new Exception(sprintf("'%s', is not a valid domain", $domain));
}

Expand All @@ -91,7 +91,7 @@ public function addLine($ip, $domain, $aliases = '')
*/
public function removeLine($domain)
{
if (!filter_var($domain, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => "/^[a-zA-Z0-9\\.]*[a-zA-Z0-9]+?/"]])) {
if (! filter_var($domain, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^[a-zA-Z0-9\\.]*[a-zA-Z0-9]+?/']])) {
throw new Exception(sprintf("'%s', is not a valid domain", $domain));
}

Expand All @@ -101,7 +101,7 @@ public function removeLine($domain)
}

/**
* Save the file
* Save the file.
*
* @param null $filePath
*/
Expand All @@ -115,21 +115,21 @@ public function save($filePath = null)
}

/**
* Read the File
* Read the File.
*/
protected function readFile()
{
$file = fopen($this->filePath, 'r');

while(($line = fgets($file)) !== false) {
while (($line = fgets($file)) !== false) {
$this->parseLine($line);
}

fclose($file);
}

/**
* Parse a line
* Parse a line.
*
* @param $line
*/
Expand All @@ -138,7 +138,6 @@ protected function parseLine($line)
$matches = $this->explodeLine($line);

if (isset($matches[1], $matches[2])) {

$ip = $matches[1];
$domainLine = $this->explodeLine($matches[2]);

Expand All @@ -152,7 +151,7 @@ protected function parseLine($line)
}

/**
* Explode entry by whitespace regex
* Explode entry by whitespace regex.
*
* @param $line
*
Expand All @@ -166,7 +165,7 @@ protected function explodeLine($line)
}

/**
* Write lines to the file
* Write lines to the file.
*
* @param $filePath
*
Expand All @@ -175,16 +174,16 @@ protected function explodeLine($line)
*/
protected function writeFile($filePath)
{
if (is_file($filePath) && !is_writable($filePath)) {
if (is_file($filePath) && ! is_writable($filePath)) {
throw new Exception(sprintf("File '%s' is not writable", $filePath));
}

$file = fopen($filePath, 'w');

foreach ($this->lines as $domain => $attributes) {
fwrite($file, $attributes['ip'] . "\t\t" . $domain ." " . $attributes['aliases'] . " \r\n");
fwrite($file, $attributes['ip']."\t\t".$domain.' '.$attributes['aliases']." \r\n");
}

fclose($file);
}
}
}

0 comments on commit bf7eebd

Please sign in to comment.