Skip to content

Commit 793ac33

Browse files
committed
Minor refactoring
1 parent 0be3f55 commit 793ac33

14 files changed

Lines changed: 87 additions & 86 deletions

docs/methods/RenderClient.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
### compatibility
1313
```php
14-
@param string $lineSeparator
14+
@param string $eol
1515
@return string
1616
```
1717
Generates an robots.txt optimized for parsing by custom 3rd party parsers, witch do not follow the standards.
@@ -23,7 +23,7 @@ Differences compared to [minimal](#minimal):
2323

2424
### compressed
2525
```php
26-
@param string $lineSeparator
26+
@param string $eol
2727
@return string
2828
```
2929
Generates an robots.txt compressed to a absolute minimum.
@@ -32,7 +32,7 @@ Best suited for parsing purposes and storage in databases.
3232

3333
### normal
3434
```php
35-
@param string $lineSeparator
35+
@param string $eol
3636
@return string
3737
```
3838
Generates an normal looking robots.txt.
@@ -44,7 +44,7 @@ Differences compared to [compressed](#compressed):
4444

4545
### minimal
4646
```php
47-
@param string $lineSeparator
47+
@param string $eol
4848
@return string
4949
```
5050
Generates an minimal but normal looking robots.txt.

src/Client/Directives/AllowClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
class AllowClient implements ClientInterface, RobotsTxtInterface
2222
{
23-
use DirectiveClientCommons;
23+
use DirectiveClientTrait;
2424

2525
/**
2626
* Paths

src/Client/Directives/DirectiveClientCommons.php renamed to src/Client/Directives/DirectiveClientTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
use DateTimeZone;
1313

1414
/**
15-
* Trait DirectiveClientCommons
15+
* Trait DirectiveClientTrait
1616
*
1717
* @package vipnytt\RobotsTxtParser\Client\Directives
1818
*/
19-
trait DirectiveClientCommons
19+
trait DirectiveClientTrait
2020
{
2121
/**
2222
* Is time between

src/Client/Directives/InlineCleanParamClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class InlineCleanParamClient implements ClientInterface
1818
{
19-
use DirectiveClientCommons;
19+
use DirectiveClientTrait;
2020

2121
/**
2222
* Clean-param

src/Client/Directives/RequestRateClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class RequestRateClient extends DelayCore implements ClientInterface, DelayInterface
1818
{
19-
use DirectiveClientCommons;
19+
use DirectiveClientTrait;
2020

2121
/**
2222
* Rates

src/Client/Directives/VisitTimeClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class VisitTimeClient implements ClientInterface
1818
{
19-
use DirectiveClientCommons;
19+
use DirectiveClientTrait;
2020

2121
/**
2222
* Times

src/Client/RenderClient.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,24 @@ public function __construct(RootDirectiveHandler $handler)
4343
* - Byte consuming, may be multiple times larger (depending on the number of user-agents)
4444
* - Maximum compatibility, optimized for badly written 3rd party parsers with limited specification support
4545
*
46-
* @param string $lineSeparator
46+
* @param string $eol
4747
* @return string
4848
*/
49-
public function compatibility($lineSeparator = "\r\n")
49+
public function compatibility($eol = "\r\n")
5050
{
51-
return $this->generate(3, $lineSeparator) . $lineSeparator;
51+
return $this->generate(3, $eol) . $eol;
5252
}
5353

5454
/**
5555
* Generate an rule string array
5656
*
5757
* @param int $level
58-
* @param string $lineSeparator
58+
* @param string $eol
5959
* @return string
6060
*/
61-
private function generate($level, $lineSeparator)
61+
private function generate($level, $eol)
6262
{
63-
$handler = new RenderHandler($level, $lineSeparator);
63+
$handler = new RenderHandler($level, $eol);
6464
if ($level === 3) {
6565
$this->root->userAgent()->render($handler);
6666
}
@@ -80,13 +80,13 @@ private function generate($level, $lineSeparator)
8080
* - The directives first character is uppercase
8181
* - Whitespace between the directive and it's value
8282
*
83-
* @param string $lineSeparator
83+
* @param string $eol
8484
* @return string
8585
*/
86-
public function minimal($lineSeparator = "\r\n")
86+
public function minimal($eol = "\r\n")
8787
{
8888

89-
return $this->generate(1, $lineSeparator);
89+
return $this->generate(1, $eol);
9090
}
9191

9292
/**
@@ -97,12 +97,12 @@ public function minimal($lineSeparator = "\r\n")
9797
* - Easy to edit
9898
* - User-agent groups are separated with blank lines
9999
*
100-
* @param string $lineSeparator
100+
* @param string $eol
101101
* @return string
102102
*/
103-
public function normal($lineSeparator = "\r\n")
103+
public function normal($eol = "\r\n")
104104
{
105-
return $this->generate(2, $lineSeparator);
105+
return $this->generate(2, $eol);
106106
}
107107

108108
/**
@@ -111,11 +111,11 @@ public function normal($lineSeparator = "\r\n")
111111
* Generates an robots.txt compressed to a absolute minimum.
112112
* Best suited for parsing purposes and storage in databases.
113113
*
114-
* @param string $lineSeparator
114+
* @param string $eol
115115
* @return string
116116
*/
117-
public function compressed($lineSeparator = PHP_EOL)
117+
public function compressed($eol = PHP_EOL)
118118
{
119-
return $this->generate(0, $lineSeparator);
119+
return $this->generate(0, $eol);
120120
}
121121
}

src/Handler/RenderHandler.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
class RenderHandler implements RobotsTxtInterface
2020
{
2121
/**
22-
* Mode
22+
* Render level
2323
* @var int
2424
*/
25-
private $mode;
25+
private $level;
2626

2727
/**
2828
* Line separator
2929
* @var string
3030
*/
31-
private $separator;
31+
private $eol;
3232

3333
/**
3434
* Rule strings
@@ -57,42 +57,42 @@ class RenderHandler implements RobotsTxtInterface
5757
/**
5858
* RenderHandler constructor.
5959
*
60-
* @param int $mode
60+
* @param int $level
6161
* @param string $lineSeparator
6262
*/
63-
public function __construct($mode, $lineSeparator = "\r\n")
63+
public function __construct($level, $lineSeparator = "\r\n")
6464
{
65-
$this->mode = $mode;
65+
$this->level = $level;
6666
$this->separatorCheck($lineSeparator);
6767
}
6868

6969
/**
7070
* Line separator check
7171
*
72-
* @param string $lineSeparator
72+
* @param string $eol
7373
* @throws ClientException
7474
*/
75-
private function separatorCheck($lineSeparator)
75+
private function separatorCheck($eol)
7676
{
77-
if (!in_array($lineSeparator, [
77+
if (!in_array($eol, [
7878
"\r\n",
7979
"\n",
8080
"\r",
8181
])
8282
) {
8383
throw new ClientException('Invalid line separator');
8484
}
85-
$this->separator = $lineSeparator;
85+
$this->eol = $eol;
8686
}
8787

8888
/**
8989
* Get mode
9090
*
9191
* @return int
9292
*/
93-
public function getMode()
93+
public function getLevel()
9494
{
95-
return $this->mode;
95+
return $this->level;
9696
}
9797

9898
/**
@@ -104,8 +104,9 @@ public function getMode()
104104
*/
105105
public function addInline($directive, RenderHandler $handler)
106106
{
107-
foreach ($handler->export() as $line) {
108-
$this->add($directive, trim($line));
107+
$lines = array_map('trim', $handler->export());
108+
foreach ($lines as $line) {
109+
$this->add($directive, $line);
109110
}
110111
return true;
111112
}
@@ -131,7 +132,7 @@ public function add($directive, $line)
131132
{
132133
$this->previous = $this->directive;
133134
$this->directive = $directive;
134-
if ($this->mode >= 2) {
135+
if ($this->level >= 2) {
135136
$this->strings[] = $this->advanced() . $line;
136137
return true;
137138
}
@@ -147,11 +148,11 @@ public function add($directive, $line)
147148
private function advanced()
148149
{
149150
$result = '';
150-
if ($this->boolInsertSeparatorMode3() ||
151+
if ($this->boolInsertSeparatorLevel3() ||
151152
$this->previous !== self::DIRECTIVE_USER_AGENT &&
152153
$this->directive === self::DIRECTIVE_USER_AGENT
153154
) {
154-
$result = $this->separator;
155+
$result = $this->eol;
155156
$this->previousRoot = $this->directive;
156157
}
157158
$result .= $this->basic();
@@ -163,9 +164,9 @@ private function advanced()
163164
*
164165
* @return bool
165166
*/
166-
private function boolInsertSeparatorMode3()
167+
private function boolInsertSeparatorLevel3()
167168
{
168-
return $this->mode >= 3 &&
169+
return $this->level >= 3 &&
169170
$this->previousRoot !== $this->directive &&
170171
in_array($this->directive, [
171172
self::DIRECTIVE_HOST,
@@ -182,7 +183,7 @@ private function boolInsertSeparatorMode3()
182183
private function basic()
183184
{
184185
$result = $this->directive . ':';
185-
if ($this->mode === 0) {
186+
if ($this->level === 0) {
186187
return $result;
187188
}
188189
return ucfirst($result) . ' ';
@@ -195,6 +196,6 @@ private function basic()
195196
*/
196197
public function generate()
197198
{
198-
return trim(implode($this->separator, $this->strings));
199+
return trim(implode($this->eol, $this->strings));
199200
}
200201
}

src/Parser/Directives/AllowParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private function removeOverlapping()
158158
public function render(RenderHandler $handler)
159159
{
160160
sort($this->path);
161-
$inline = new RenderHandler($handler->getMode());
161+
$inline = new RenderHandler($handler->getLevel());
162162
$this->host->render($inline);
163163
$this->cleanParam->render($inline);
164164
$handler->addInline($this->directive, $inline);

src/Parser/Directives/CleanParamParserCore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function add($line)
6464
public function render(RenderHandler $handler)
6565
{
6666
ksort($this->cleanParam);
67-
return $handler->getMode() >= 3 ? $this->renderExtensive($handler) : $this->renderCompressed($handler);
67+
return $handler->getLevel() >= 3 ? $this->renderExtensive($handler) : $this->renderCompressed($handler);
6868
}
6969

7070
/**

0 commit comments

Comments
 (0)