Skip to content

Commit bd2a8e8

Browse files
Martijn van der Leealexander-nitsche
Martijn van der Lee
authored andcommitted
Added splitWords and various code quality improvements
1 parent a8f336f commit bd2a8e8

File tree

9 files changed

+378
-159
lines changed

9 files changed

+378
-159
lines changed

phpunit.xml

+18-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit colors="true">
3-
<testsuites>
4-
<testsuite name="syllable">
5-
<directory>tests/src</directory>
6-
</testsuite>
7-
<testsuite name="syllable-build">
8-
<directory>tests/build</directory>
9-
</testsuite>
10-
</testsuites>
11-
12-
<filter>
13-
<whitelist>
14-
<directory>src</directory>
15-
<directory>build/classes</directory>
16-
</whitelist>
17-
<blacklist>
18-
<directory>tests</directory>
19-
</blacklist>
20-
</filter>
21-
</phpunit>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit colors="true">
3+
<testsuites>
4+
<testsuite name="syllable">
5+
<directory>tests/src</directory>
6+
</testsuite>
7+
<testsuite name="syllable-build">
8+
<directory>tests/build</directory>
9+
</testsuite>
10+
</testsuites>
11+
12+
<filter>
13+
<whitelist>
14+
<directory>src</directory>
15+
<directory>build/classes</directory>
16+
</whitelist>
17+
</filter>
18+
</phpunit>

src/Cache/Json.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ protected function decode($array)
1919

2020
protected function getFilename($language)
2121
{
22-
return "syllable.{$language}.json";
22+
return "syllable.$language.json";
2323
}
2424
}

src/Cache/Serialized.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ protected function decode($array)
1919

2020
protected function getFilename($language)
2121
{
22-
return "syllable.{$language}.serialized";
22+
return "syllable.$language.serialized";
2323
}
2424
}

src/Hyphen/Entity.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Vanderlee\Syllable\Hyphen;
44

5+
use DOMNode;
6+
57
class Entity implements Hyphen
68
{
79
private $entity;
@@ -16,13 +18,20 @@ public function joinText($parts)
1618
return join('&'.$this->entity.';', $parts);
1719
}
1820

19-
public function joinHtmlDom($parts, \DOMNode $node)
21+
public function joinHtmlDom($parts, DOMNode $node)
2022
{
2123
if (($p = count($parts)) > 1) {
2224
$node->textContent = $parts[--$p];
2325
while (--$p >= 0) {
24-
$node = $node->parentNode->insertBefore($node->ownerDocument->createEntityReference($this->entity), $node);
25-
$node = $node->parentNode->insertBefore($node->ownerDocument->createTextNode($parts[$p]), $node);
26+
$node = $node->parentNode->insertBefore(
27+
$node->ownerDocument->createEntityReference($this->entity),
28+
$node
29+
);
30+
31+
$node = $node->parentNode->insertBefore(
32+
$node->ownerDocument->createTextNode($parts[$p]),
33+
$node
34+
);
2635
}
2736
}
2837
}

src/Hyphen/Hyphen.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Vanderlee\Syllable\Hyphen;
44

5+
use DOMNode;
6+
57
interface Hyphen
68
{
79
public function joinText($parts);
810

9-
public function joinHtmlDom($parts, \DOMNode $node);
11+
public function joinHtmlDom($parts, DOMNode $node);
1012

1113
public function stripHtml($html);
1214
}

src/Hyphen/Text.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Vanderlee\Syllable\Hyphen;
44

5+
use DOMNode;
6+
57
class Text implements Hyphen
68
{
79
private $text;
@@ -16,7 +18,7 @@ public function joinText($parts)
1618
return join($this->text, $parts);
1719
}
1820

19-
public function joinHtmlDom($parts, \DOMNode $node)
21+
public function joinHtmlDom($parts, DOMNode $node)
2022
{
2123
$node->textContent = $this->joinText($parts);
2224
}

src/Source/File.php

+18-18
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class File implements Source
1111
private static $minHyphens = null;
1212
private $path = null;
1313
private $language = null;
14-
private $loaded = false;
14+
private $loaded;
1515
private $patterns = null;
16-
private $max_pattern_length = null;
16+
private $maxPatternLength = null;
1717
private $hyphenations = null;
1818

1919
public function __construct($language, $path)
@@ -38,7 +38,7 @@ public function setLanguage($language)
3838
public function getMinHyphens()
3939
{
4040
if (!self::$minHyphens) {
41-
self::$minHyphens = json_decode(file_get_contents("{$this->path}/min.json"), true);
41+
self::$minHyphens = json_decode(file_get_contents("$this->path/min.json"), true);
4242
}
4343

4444
return isset(self::$minHyphens[$this->language]) ? self::$minHyphens[$this->language] : null;
@@ -48,18 +48,18 @@ private function loadLanguage()
4848
{
4949
if (!$this->loaded) {
5050
$this->patterns = [];
51-
$this->max_pattern_length = 0;
51+
$this->maxPatternLength = 0;
5252
$this->hyphenations = [];
5353

5454
// parser state
5555
$command = false;
5656
$braces = false;
5757

5858
// parse .tex file
59-
foreach (file("{$this->path}/hyph-{$this->language}.tex") as $line) {
59+
foreach (file("$this->path/hyph-$this->language.tex") as $line) {
6060
$offset = 0;
61-
$strlen_line = mb_strlen($line);
62-
while ($offset < $strlen_line) {
61+
$strlenLine = mb_strlen($line);
62+
while ($offset < $strlenLine) {
6363
$char = $line[$offset];
6464

6565
// %comment
@@ -74,7 +74,7 @@ private function loadLanguage()
7474
continue; // next token
7575
}
7676

77-
// {
77+
// opening brace
7878
if ($char === '{') {
7979
$braces = true;
8080
$offset++;
@@ -89,43 +89,43 @@ private function loadLanguage()
8989
$numbers = '';
9090
$pattern = '';
9191
$strlen = 0;
92-
$expect_number = true;
92+
$expectNumber = true;
9393
foreach (preg_split('/(?<!^)(?!$)/u', $m[0]) as $char) {
9494
if (is_numeric($char)) {
9595
$numbers .= $char;
96-
$expect_number = false;
96+
$expectNumber = false;
9797
} else {
98-
if ($expect_number) {
98+
if ($expectNumber) {
9999
$numbers .= '0';
100100
}
101101
$pattern .= $char;
102102
$strlen++;
103-
$expect_number = true;
103+
$expectNumber = true;
104104
}
105105
$offset++;
106106
}
107-
if ($expect_number) {
107+
if ($expectNumber) {
108108
$numbers .= '0';
109109
}
110110

111111
$this->patterns[$pattern] = $numbers;
112-
if ($strlen > $this->max_pattern_length) {
113-
$this->max_pattern_length = $strlen;
112+
if ($strlen > $this->maxPatternLength) {
113+
$this->maxPatternLength = $strlen;
114114
}
115115
}
116116
continue 3; // next token
117117

118118
case 'hyphenation':
119119
if (preg_match('~^\S+~u', substr($line, $offset), $m) === 1) {
120-
$hyphenation = preg_replace('~\-~', '', $m[0]);
120+
$hyphenation = str_replace('-', '', $m[0]);
121121
$this->hyphenations[$hyphenation] = $m[0];
122122
$offset += strlen($m[0]);
123123
}
124124
continue 3; // next token
125125
}
126126
}
127127

128-
// }
128+
// closing brace
129129
if ($char === '}') {
130130
$braces = false;
131131
$command = false;
@@ -153,7 +153,7 @@ public function getMaxPattern()
153153
{
154154
$this->loadLanguage();
155155

156-
return $this->max_pattern_length;
156+
return $this->maxPatternLength;
157157
}
158158

159159
public function getPatterns()

0 commit comments

Comments
 (0)