Skip to content

Commit 5f3ff95

Browse files
committed
Simplifies some code, reformats some code
1 parent bbe73a2 commit 5f3ff95

File tree

6 files changed

+64
-57
lines changed

6 files changed

+64
-57
lines changed

library/Pdp/Parser.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function parseHost($host)
9595
$registerableDomain = null;
9696
$publicSuffix = null;
9797

98-
// Fixes #22: Single label domains are set as Host::$host and all other
98+
// Fixes #22: Single label domains are set as Host::$host and all other
9999
// properties are null.
100100
if (strpos($host, '.') !== false) {
101101
$subdomain = $this->getSubdomain($host);
@@ -123,8 +123,8 @@ public function getPublicSuffix($host)
123123
return null;
124124
}
125125

126-
// Fixes #22: If a single label domain makes it this far (e.g.,
127-
// localhost, foo, etc.), this stops it from incorrectly being set as
126+
// Fixes #22: If a single label domain makes it this far (e.g.,
127+
// localhost, foo, etc.), this stops it from incorrectly being set as
128128
// the public suffix.
129129
if (strpos($host, '.') === false) {
130130
return null;

library/Pdp/PublicSuffixList.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ class PublicSuffixList extends \ArrayObject
1818
/**
1919
* Public constructor
2020
*
21-
* @param mixed $list Array representing Public Suffix List or PHP Public
22-
* Suffix List file
23-
* @throws \InvalidArgumentException If $list is not array, file did not
24-
* contain an array, or $list is not an object
21+
* @param mixed $list Array representing Public Suffix List or PHP Public Suffix List file
2522
*/
2623
public function __construct($list)
2724
{

library/Pdp/PublicSuffixListManager.php

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,23 @@ public function __construct($cacheDir = null)
6565
public function refreshPublicSuffixList()
6666
{
6767
$this->fetchListFromSource();
68-
$publicSuffixListArray = $this->parseListToArray($this->cacheDir . '/' . self::PDP_PSL_TEXT_FILE);
68+
$publicSuffixListArray = $this->parseListToArray(
69+
$this->cacheDir . '/' . self::PDP_PSL_TEXT_FILE
70+
);
6971
$this->writePhpCache($publicSuffixListArray);
7072
}
7173

7274
/**
7375
* Obtain Public Suffix List from its online source and write to cache dir
7476
*
77+
* TODO: Should throw exception if empty/null/content-length too short
78+
*
7579
* @return int Number of bytes that were written to the file
7680
*/
7781
public function fetchListFromSource()
7882
{
79-
$publicSuffixList = $this->getHttpAdapter()->getContent($this->publicSuffixListUrl);
83+
$publicSuffixList = $this->getHttpAdapter()
84+
->getContent($this->publicSuffixListUrl);
8085

8186
return $this->write(self::PDP_PSL_TEXT_FILE, $publicSuffixList);
8287
}
@@ -91,7 +96,7 @@ public function fetchListFromSource()
9196
*
9297
* @param string $textFile Public Suffix List text filename
9398
* @return array Associative, multidimensional array representation of the
94-
* public suffx list
99+
* public suffx list
95100
*/
96101
public function parseListToArray($textFile)
97102
{
@@ -100,7 +105,7 @@ public function parseListToArray($textFile)
100105
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
101106
);
102107

103-
$data = array_filter($data, function($line) {
108+
$data = array_filter($data, function ($line) {
104109
return strstr($line, '//') === false;
105110
});
106111

@@ -123,9 +128,9 @@ public function parseListToArray($textFile)
123128
* distribution
124129
*
125130
* @param array $publicSuffixListArray Initially an empty array, this eventually
126-
* becomes the array representation of the Public Suffix List
127-
* @param array $ruleParts One line (rule) from the Public Suffix List
128-
* exploded on '.', or the remaining portion of that array during recursion
131+
* becomes the array representation of the Public Suffix List
132+
* @param array $ruleParts One line (rule) from the Public Suffix List
133+
* exploded on '.', or the remaining portion of that array during recursion
129134
*/
130135
public function buildArray(array &$publicSuffixListArray, array $ruleParts)
131136
{
@@ -171,35 +176,15 @@ public function writePhpCache(array $publicSuffixList)
171176
*/
172177
public function getList()
173178
{
174-
if ($this->list == null) {
175-
if (!$this->cacheExist()) {
176-
$this->refreshPublicSuffixList();
177-
}
178-
179-
$list = include $this->cacheDir . '/' . self::PDP_PSL_PHP_FILE;
180-
181-
if ($list === false) {
182-
throw new \Exception("Cannot read '" . $this->cacheDir . "/" . self::PDP_PSL_PHP_FILE . "'");
183-
}
184-
185-
$this->list = new PublicSuffixList($list);
179+
if (!file_exists($this->cacheDir . '/' . self::PDP_PSL_PHP_FILE)) {
180+
$this->refreshPublicSuffixList();
186181
}
187182

188-
return $this->list;
189-
}
183+
$this->list = new PublicSuffixList(
184+
include $this->cacheDir . '/' . self::PDP_PSL_PHP_FILE
185+
);
190186

191-
/**
192-
* Verifies the existence of the cache file
193-
*
194-
* @return bool TRUE if the cache file exists; FALSE otherwise.
195-
*/
196-
protected function cacheExist()
197-
{
198-
if (file_exists($this->cacheDir . '/' . self::PDP_PSL_PHP_FILE) === true) {
199-
return true;
200-
} else {
201-
return false;
202-
}
187+
return $this->list;
203188
}
204189

205190
/**

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
ini_set('display_errors', 1);
55
ini_set('display_startup_errors', 1);
66

7-
date_default_timezone_set('America/Chicago');
7+
date_default_timezone_set('UTC');
88

99
require_once __DIR__ . '/../vendor/autoload.php';
1010

tests/library/Pdp/HttpAdapter/CurlHttpAdapterTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,4 @@ public function testGetContent()
3232
$this->assertNotNull($content);
3333
$this->assertContains('google', $content);
3434
}
35-
3635
}
37-

tests/library/Pdp/PublicSuffixListManagerTest.php

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,37 @@ protected function tearDown()
7474

7575
public function testRefreshPublicSuffixList()
7676
{
77-
$content = file_get_contents($this->dataDir . '/' . PublicSuffixListManager::PDP_PSL_TEXT_FILE);
77+
$content = file_get_contents(
78+
$this->dataDir . '/' . PublicSuffixListManager::PDP_PSL_TEXT_FILE
79+
);
7880

7981
$this->httpAdapter->expects($this->once())
8082
->method('getContent')
8183
->with($this->publicSuffixListUrl)
8284
->will($this->returnValue($content));
8385

84-
$this->assertFalse(file_exists($this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_TEXT_FILE));
85-
$this->assertFalse(file_exists($this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE));
86+
$this->assertFileNotExists(
87+
$this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_TEXT_FILE
88+
);
89+
$this->assertFileNotExists(
90+
$this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE
91+
);
8692

8793
$this->listManager->refreshPublicSuffixList();
8894

89-
$this->assertFileExists($this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_TEXT_FILE);
90-
$this->assertFileExists($this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE);
95+
$this->assertFileExists(
96+
$this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_TEXT_FILE
97+
);
98+
$this->assertFileExists(
99+
$this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE
100+
);
91101
}
92102

93103
public function testFetchListFromSource()
94104
{
95-
$content = file_get_contents($this->dataDir . '/' . PublicSuffixListManager::PDP_PSL_TEXT_FILE);
105+
$content = file_get_contents(
106+
$this->dataDir . '/' . PublicSuffixListManager::PDP_PSL_TEXT_FILE
107+
);
96108

97109
$this->httpAdapter->expects($this->once())
98110
->method('getContent')
@@ -106,17 +118,24 @@ public function testFetchListFromSource()
106118
public function testGetHttpAdapterReturnsDefaultCurlAdapterIfAdapterNotSet()
107119
{
108120
$listManager = new PublicSuffixListManager($this->cacheDir);
109-
$this->assertInstanceOf('\Pdp\HttpAdapter\CurlHttpAdapter', $listManager->getHttpAdapter());
121+
$this->assertInstanceOf(
122+
'\Pdp\HttpAdapter\CurlHttpAdapter',
123+
$listManager->getHttpAdapter()
124+
);
110125
}
111126

112127
public function testWritePhpCache()
113128
{
114-
$this->assertFalse(file_exists($this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE));
129+
$this->assertFileNotExists(
130+
$this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE
131+
);
115132
$array = $this->listManager->parseListToArray(
116133
$this->dataDir . '/' . PublicSuffixListManager::PDP_PSL_TEXT_FILE
117134
);
118135
$this->assertGreaterThanOrEqual(230000, $this->listManager->writePhpCache($array));
119-
$this->assertFileExists($this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE);
136+
$this->assertFileExists(
137+
$this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE
138+
);
120139
$publicSuffixList = include $this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE;
121140
$this->assertInternalType('array', $publicSuffixList);
122141
$this->assertGreaterThanOrEqual(300, count($publicSuffixList));
@@ -145,7 +164,9 @@ public function testGetList()
145164
$this->dataDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE,
146165
$this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE
147166
);
148-
$this->assertFileExists($this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE);
167+
$this->assertFileExists(
168+
$this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE
169+
);
149170
$publicSuffixList = $this->listManager->getList();
150171
$this->assertInstanceOf('\Pdp\PublicSuffixList', $publicSuffixList);
151172
$this->assertGreaterThanOrEqual(300, count($publicSuffixList));
@@ -155,10 +176,15 @@ public function testGetList()
155176

156177
public function testGetListWithoutCache()
157178
{
158-
unlink($this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE);
159-
$this->assertFileNotExists($this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE);
179+
$this->assertFileNotExists(
180+
$this->cacheDir . '/' . PublicSuffixListManager::PDP_PSL_PHP_FILE
181+
);
160182

161-
$listManager = $this->getMock('\Pdp\PublicSuffixListManager', array('refreshPublicSuffixList'), array($this->cacheDir));
183+
$listManager = $this->getMock(
184+
'\Pdp\PublicSuffixListManager',
185+
array('refreshPublicSuffixList'),
186+
array($this->cacheDir)
187+
);
162188

163189
$dataDir = $this->dataDir;
164190
$cacheDir = $this->cacheDir;
@@ -172,7 +198,8 @@ public function testGetListWithoutCache()
172198
);
173199
}));
174200

175-
$listManager->getList();
201+
$publicSuffixList = $listManager->getList();
202+
$this->assertInstanceOf('\Pdp\PublicSuffixList', $publicSuffixList);
176203
}
177204

178205
public function testGetProvidedListFromDefaultCacheDir()

0 commit comments

Comments
 (0)