Skip to content

Commit

Permalink
Code optimizions
Browse files Browse the repository at this point in the history
- Remove unused variables in the Comment-classes (after last commit)
- ErrorHandler class - short variable name fix (CodeClimate recommendation)
- Sort optimizions
- Test for higher coverage
  • Loading branch information
JanPetterMG committed Aug 21, 2016
1 parent 89e6235 commit a2c6372
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 70 deletions.
12 changes: 2 additions & 10 deletions src/Client/Directives/CommentClient.php
Expand Up @@ -18,12 +18,6 @@
*/
class CommentClient implements ClientInterface, RobotsTxtInterface
{
/**
* Base uri
* @var string
*/
private $base;

/**
* User-agent
* @var string
Expand All @@ -39,13 +33,11 @@ class CommentClient implements ClientInterface, RobotsTxtInterface
/**
* CommentClient constructor.
*
* @param string $base
* @param string $userAgent
* @param array $comments
*/
public function __construct($base, $userAgent, array $comments)
public function __construct($userAgent, array $comments)
{
$this->base = $base;
$this->userAgent = $userAgent;
$this->comments = $comments;
}
Expand All @@ -57,7 +49,7 @@ public function __construct($base, $userAgent, array $comments)
*/
public function get()
{
return $this->userAgent == self::USER_AGENT ? [] : $this->export();
return $this->userAgent === self::USER_AGENT ? [] : $this->export();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/Directives/SubDirectiveHandler.php
Expand Up @@ -88,7 +88,7 @@ public function __construct($base, $effective, $userAgent)
{
$this->allow = new AllowParser($base, $effective, self::DIRECTIVE_ALLOW);
$this->cacheDelay = new DelayParser($base, self::DIRECTIVE_CACHE_DELAY);
$this->comment = new CommentParser($base, $userAgent);
$this->comment = new CommentParser($userAgent);
$this->crawlDelay = new DelayParser($base, self::DIRECTIVE_CRAWL_DELAY);
$this->disallow = new AllowParser($base, $effective, self::DIRECTIVE_DISALLOW);
$this->noIndex = new AllowParser($base, $effective, self::DIRECTIVE_NO_INDEX);
Expand Down
22 changes: 11 additions & 11 deletions src/Handler/ErrorHandler.php
Expand Up @@ -31,21 +31,21 @@ public function __construct()
/**
* Callback
*
* @param int $no
* @param string $str
* @param string $file
* @param int $line
* @param array $context
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @param array $errcontext
* @return bool
*/
public function callback($no, $str, $file = '', $line = 0, $context = [])
public function callback($errno, $errstr, $errfile = '', $errline = 0, $errcontext = [])
{
$this->log[(string)microtime(true)] = [
'no' => $no,
'str' => $str,
'file' => $file,
'line' => $line,
'context' => $context,
'no' => $errno,
'str' => $errstr,
'file' => $errfile,
'line' => $errline,
'context' => $errcontext,
];
return $this->handle();
}
Expand Down
26 changes: 12 additions & 14 deletions src/Parser/Directives/AllowParser.php
Expand Up @@ -121,9 +121,7 @@ private function addPath($path)
*/
public function render(RenderHandler $handler)
{
if (!$this->optimized) {
$this->removeOverlapping();
}
$this->removeOverlapping();
sort($this->path);
$inline = new RenderHandler($handler->getLevel());
$this->host->render($inline);
Expand All @@ -142,17 +140,19 @@ public function render(RenderHandler $handler)
*/
private function removeOverlapping()
{
foreach ($this->path as $key1 => &$path1) {
foreach ($this->path as $key2 => &$path2) {
if ($key1 !== $key2 &&
mb_strpos($path1, $path2) === 0
) {
unset($this->path[$key1]);
if (!$this->optimized) {
foreach ($this->path as $key1 => &$path1) {
foreach ($this->path as $key2 => &$path2) {
if ($key1 !== $key2 &&
mb_strpos($path1, $path2) === 0
) {
unset($this->path[$key1]);
}
}
}
$this->optimized = true;
}
$this->optimized = true;
return true;
return $this->optimized;
}

/**
Expand All @@ -162,9 +162,7 @@ private function removeOverlapping()
*/
public function client()
{
if (!$this->optimized) {
$this->removeOverlapping();
}
$this->removeOverlapping();
return new AllowClient($this->path, $this->host->client(), $this->cleanParam->client());
}
}
12 changes: 2 additions & 10 deletions src/Parser/Directives/CommentParser.php
Expand Up @@ -25,12 +25,6 @@ class CommentParser implements ParserInterface, RobotsTxtInterface
*/
private $userAgent;

/**
* Base uri
* @var string
*/
private $base;

/**
* Comment array
* @var string[]
Expand All @@ -40,12 +34,10 @@ class CommentParser implements ParserInterface, RobotsTxtInterface
/**
* Comment constructor.
*
* @param string $base
* @param string $userAgent
*/
public function __construct($base, $userAgent)
public function __construct($userAgent)
{
$this->base = $base;
$this->userAgent = $userAgent;
}

Expand All @@ -68,7 +60,7 @@ public function add($line)
*/
public function client()
{
return new CommentClient($this->base, $this->userAgent, $this->comments);
return new CommentClient($this->userAgent, $this->comments);
}

/**
Expand Down
21 changes: 10 additions & 11 deletions src/Parser/Directives/RequestRateParser.php
Expand Up @@ -113,9 +113,7 @@ private function draftParseRate($string)
*/
public function client($userAgent = self::USER_AGENT, $fallbackValue = 0)
{
if (!$this->sorted) {
$this->sort();
}
$this->sort();
return new RequestRateClient($this->base, $userAgent, $this->requestRates, $fallbackValue);
}

Expand All @@ -126,11 +124,14 @@ public function client($userAgent = self::USER_AGENT, $fallbackValue = 0)
*/
private function sort()
{
$this->sorted = true;
return usort($this->requestRates, function (array $requestRateA, array $requestRateB) {
// PHP 7: Switch to the <=> "Spaceship" operator
return $requestRateB['rate'] > $requestRateA['rate'];
});
if (!$this->sorted) {
$this->sorted = true;
return usort($this->requestRates, function (array $requestRateA, array $requestRateB) {
// PHP 7: Switch to the <=> "Spaceship" operator
return $requestRateB['rate'] > $requestRateA['rate'];
});
}
return $this->sorted;
}

/**
Expand All @@ -141,9 +142,7 @@ private function sort()
*/
public function render(RenderHandler $handler)
{
if (!$this->sorted) {
$this->sort();
}
$this->sort();
foreach ($this->requestRates as $array) {
$suffix = 's';
if (isset($array['from']) &&
Expand Down
37 changes: 24 additions & 13 deletions src/Parser/Directives/VisitTimeParser.php
Expand Up @@ -27,6 +27,12 @@ class VisitTimeParser implements ParserInterface, RobotsTxtInterface
*/
private $visitTimes = [];

/**
* Sorted
* @var bool
*/
private $sorted = false;

/**
* VisitTime constructor.
*/
Expand Down Expand Up @@ -57,34 +63,39 @@ public function add($line)
*/
public function client()
{
$this->sort();
return new VisitTimeClient($this->visitTimes);
}

/**
* Render
* Sort
*
* @param RenderHandler $handler
* @return bool
*/
public function render(RenderHandler $handler)
private function sort()
{
$this->sort();
foreach ($this->visitTimes as $array) {
$handler->add(self::DIRECTIVE_VISIT_TIME, $array['from'] . '-' . $array['to']);
if (!$this->sorted) {
$this->sorted = true;
return usort($this->visitTimes, function (array $visitTimeA, array $visitTimeB) {
// PHP 7: Switch to the <=> "Spaceship" operator
return $visitTimeA['from'] > $visitTimeB['from'];
});
}
return true;
return $this->sorted;
}

/**
* Sort
* Render
*
* @param RenderHandler $handler
* @return bool
*/
private function sort()
public function render(RenderHandler $handler)
{
return usort($this->visitTimes, function (array $visitTimeA, array $visitTimeB) {
// PHP 7: Switch to the <=> "Spaceship" operator
return $visitTimeA['from'] > $visitTimeB['from'];
});
$this->sort();
foreach ($this->visitTimes as $array) {
$handler->add(self::DIRECTIVE_VISIT_TIME, $array['from'] . '-' . $array['to']);
}
return true;
}
}
2 changes: 2 additions & 0 deletions tests/CommentTest.php
Expand Up @@ -33,6 +33,8 @@ public function testComment($robotsTxtContent, $result, $rendered)
$this->assertTrue($parser->userAgent('receiver')->isDisallowed("/"));
$this->assertFalse($parser->userAgent('receiver')->isAllowed("/"));

$this->assertEquals($parser->userAgent('*')->comment()->get(), []);

if ($rendered !== false) {
$this->assertEquals($rendered, $parser->render()->normal("\n"));
$this->testComment($rendered, $result, false);
Expand Down

0 comments on commit a2c6372

Please sign in to comment.