Skip to content

Commit a2c6372

Browse files
committed
Code optimizions
- Remove unused variables in the Comment-classes (after last commit) - ErrorHandler class - short variable name fix (CodeClimate recommendation) - Sort optimizions - Test for higher coverage
1 parent 89e6235 commit a2c6372

8 files changed

Lines changed: 64 additions & 70 deletions

File tree

src/Client/Directives/CommentClient.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818
*/
1919
class CommentClient implements ClientInterface, RobotsTxtInterface
2020
{
21-
/**
22-
* Base uri
23-
* @var string
24-
*/
25-
private $base;
26-
2721
/**
2822
* User-agent
2923
* @var string
@@ -39,13 +33,11 @@ class CommentClient implements ClientInterface, RobotsTxtInterface
3933
/**
4034
* CommentClient constructor.
4135
*
42-
* @param string $base
4336
* @param string $userAgent
4437
* @param array $comments
4538
*/
46-
public function __construct($base, $userAgent, array $comments)
39+
public function __construct($userAgent, array $comments)
4740
{
48-
$this->base = $base;
4941
$this->userAgent = $userAgent;
5042
$this->comments = $comments;
5143
}
@@ -57,7 +49,7 @@ public function __construct($base, $userAgent, array $comments)
5749
*/
5850
public function get()
5951
{
60-
return $this->userAgent == self::USER_AGENT ? [] : $this->export();
52+
return $this->userAgent === self::USER_AGENT ? [] : $this->export();
6153
}
6254

6355
/**

src/Handler/Directives/SubDirectiveHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function __construct($base, $effective, $userAgent)
8888
{
8989
$this->allow = new AllowParser($base, $effective, self::DIRECTIVE_ALLOW);
9090
$this->cacheDelay = new DelayParser($base, self::DIRECTIVE_CACHE_DELAY);
91-
$this->comment = new CommentParser($base, $userAgent);
91+
$this->comment = new CommentParser($userAgent);
9292
$this->crawlDelay = new DelayParser($base, self::DIRECTIVE_CRAWL_DELAY);
9393
$this->disallow = new AllowParser($base, $effective, self::DIRECTIVE_DISALLOW);
9494
$this->noIndex = new AllowParser($base, $effective, self::DIRECTIVE_NO_INDEX);

src/Handler/ErrorHandler.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ public function __construct()
3131
/**
3232
* Callback
3333
*
34-
* @param int $no
35-
* @param string $str
36-
* @param string $file
37-
* @param int $line
38-
* @param array $context
34+
* @param int $errno
35+
* @param string $errstr
36+
* @param string $errfile
37+
* @param int $errline
38+
* @param array $errcontext
3939
* @return bool
4040
*/
41-
public function callback($no, $str, $file = '', $line = 0, $context = [])
41+
public function callback($errno, $errstr, $errfile = '', $errline = 0, $errcontext = [])
4242
{
4343
$this->log[(string)microtime(true)] = [
44-
'no' => $no,
45-
'str' => $str,
46-
'file' => $file,
47-
'line' => $line,
48-
'context' => $context,
44+
'no' => $errno,
45+
'str' => $errstr,
46+
'file' => $errfile,
47+
'line' => $errline,
48+
'context' => $errcontext,
4949
];
5050
return $this->handle();
5151
}

src/Parser/Directives/AllowParser.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ private function addPath($path)
121121
*/
122122
public function render(RenderHandler $handler)
123123
{
124-
if (!$this->optimized) {
125-
$this->removeOverlapping();
126-
}
124+
$this->removeOverlapping();
127125
sort($this->path);
128126
$inline = new RenderHandler($handler->getLevel());
129127
$this->host->render($inline);
@@ -142,17 +140,19 @@ public function render(RenderHandler $handler)
142140
*/
143141
private function removeOverlapping()
144142
{
145-
foreach ($this->path as $key1 => &$path1) {
146-
foreach ($this->path as $key2 => &$path2) {
147-
if ($key1 !== $key2 &&
148-
mb_strpos($path1, $path2) === 0
149-
) {
150-
unset($this->path[$key1]);
143+
if (!$this->optimized) {
144+
foreach ($this->path as $key1 => &$path1) {
145+
foreach ($this->path as $key2 => &$path2) {
146+
if ($key1 !== $key2 &&
147+
mb_strpos($path1, $path2) === 0
148+
) {
149+
unset($this->path[$key1]);
150+
}
151151
}
152152
}
153+
$this->optimized = true;
153154
}
154-
$this->optimized = true;
155-
return true;
155+
return $this->optimized;
156156
}
157157

158158
/**
@@ -162,9 +162,7 @@ private function removeOverlapping()
162162
*/
163163
public function client()
164164
{
165-
if (!$this->optimized) {
166-
$this->removeOverlapping();
167-
}
165+
$this->removeOverlapping();
168166
return new AllowClient($this->path, $this->host->client(), $this->cleanParam->client());
169167
}
170168
}

src/Parser/Directives/CommentParser.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ class CommentParser implements ParserInterface, RobotsTxtInterface
2525
*/
2626
private $userAgent;
2727

28-
/**
29-
* Base uri
30-
* @var string
31-
*/
32-
private $base;
33-
3428
/**
3529
* Comment array
3630
* @var string[]
@@ -40,12 +34,10 @@ class CommentParser implements ParserInterface, RobotsTxtInterface
4034
/**
4135
* Comment constructor.
4236
*
43-
* @param string $base
4437
* @param string $userAgent
4538
*/
46-
public function __construct($base, $userAgent)
39+
public function __construct($userAgent)
4740
{
48-
$this->base = $base;
4941
$this->userAgent = $userAgent;
5042
}
5143

@@ -68,7 +60,7 @@ public function add($line)
6860
*/
6961
public function client()
7062
{
71-
return new CommentClient($this->base, $this->userAgent, $this->comments);
63+
return new CommentClient($this->userAgent, $this->comments);
7264
}
7365

7466
/**

src/Parser/Directives/RequestRateParser.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ private function draftParseRate($string)
113113
*/
114114
public function client($userAgent = self::USER_AGENT, $fallbackValue = 0)
115115
{
116-
if (!$this->sorted) {
117-
$this->sort();
118-
}
116+
$this->sort();
119117
return new RequestRateClient($this->base, $userAgent, $this->requestRates, $fallbackValue);
120118
}
121119

@@ -126,11 +124,14 @@ public function client($userAgent = self::USER_AGENT, $fallbackValue = 0)
126124
*/
127125
private function sort()
128126
{
129-
$this->sorted = true;
130-
return usort($this->requestRates, function (array $requestRateA, array $requestRateB) {
131-
// PHP 7: Switch to the <=> "Spaceship" operator
132-
return $requestRateB['rate'] > $requestRateA['rate'];
133-
});
127+
if (!$this->sorted) {
128+
$this->sorted = true;
129+
return usort($this->requestRates, function (array $requestRateA, array $requestRateB) {
130+
// PHP 7: Switch to the <=> "Spaceship" operator
131+
return $requestRateB['rate'] > $requestRateA['rate'];
132+
});
133+
}
134+
return $this->sorted;
134135
}
135136

136137
/**
@@ -141,9 +142,7 @@ private function sort()
141142
*/
142143
public function render(RenderHandler $handler)
143144
{
144-
if (!$this->sorted) {
145-
$this->sort();
146-
}
145+
$this->sort();
147146
foreach ($this->requestRates as $array) {
148147
$suffix = 's';
149148
if (isset($array['from']) &&

src/Parser/Directives/VisitTimeParser.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class VisitTimeParser implements ParserInterface, RobotsTxtInterface
2727
*/
2828
private $visitTimes = [];
2929

30+
/**
31+
* Sorted
32+
* @var bool
33+
*/
34+
private $sorted = false;
35+
3036
/**
3137
* VisitTime constructor.
3238
*/
@@ -57,34 +63,39 @@ public function add($line)
5763
*/
5864
public function client()
5965
{
66+
$this->sort();
6067
return new VisitTimeClient($this->visitTimes);
6168
}
6269

6370
/**
64-
* Render
71+
* Sort
6572
*
66-
* @param RenderHandler $handler
6773
* @return bool
6874
*/
69-
public function render(RenderHandler $handler)
75+
private function sort()
7076
{
71-
$this->sort();
72-
foreach ($this->visitTimes as $array) {
73-
$handler->add(self::DIRECTIVE_VISIT_TIME, $array['from'] . '-' . $array['to']);
77+
if (!$this->sorted) {
78+
$this->sorted = true;
79+
return usort($this->visitTimes, function (array $visitTimeA, array $visitTimeB) {
80+
// PHP 7: Switch to the <=> "Spaceship" operator
81+
return $visitTimeA['from'] > $visitTimeB['from'];
82+
});
7483
}
75-
return true;
84+
return $this->sorted;
7685
}
7786

7887
/**
79-
* Sort
88+
* Render
8089
*
90+
* @param RenderHandler $handler
8191
* @return bool
8292
*/
83-
private function sort()
93+
public function render(RenderHandler $handler)
8494
{
85-
return usort($this->visitTimes, function (array $visitTimeA, array $visitTimeB) {
86-
// PHP 7: Switch to the <=> "Spaceship" operator
87-
return $visitTimeA['from'] > $visitTimeB['from'];
88-
});
95+
$this->sort();
96+
foreach ($this->visitTimes as $array) {
97+
$handler->add(self::DIRECTIVE_VISIT_TIME, $array['from'] . '-' . $array['to']);
98+
}
99+
return true;
89100
}
90101
}

tests/CommentTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function testComment($robotsTxtContent, $result, $rendered)
3333
$this->assertTrue($parser->userAgent('receiver')->isDisallowed("/"));
3434
$this->assertFalse($parser->userAgent('receiver')->isAllowed("/"));
3535

36+
$this->assertEquals($parser->userAgent('*')->comment()->get(), []);
37+
3638
if ($rendered !== false) {
3739
$this->assertEquals($rendered, $parser->render()->normal("\n"));
3840
$this->testComment($rendered, $result, false);

0 commit comments

Comments
 (0)