Skip to content

Commit 18f0477

Browse files
frasteljbowens
authored andcommitted
Cleaned up code with the help of php-cs-fixer, fixed PHPUnit version (#75)
1 parent 6ceca8c commit 18f0477

34 files changed

+119
-137
lines changed

JBBCode/CodeDefinition.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static function construct($tagName, $replacementText, $useOption = false,
5252
$def->optionValidator = $optionValidator;
5353
$def->bodyValidator = $bodyValidator;
5454
return $def;
55-
}
55+
}
5656

5757
/**
5858
* Constructs a new CodeDefinition.
@@ -86,8 +86,8 @@ public function hasValidInputs(ElementNode $el)
8686
if ($this->usesOption() && $this->optionValidator) {
8787
$att = $el->getAttribute();
8888

89-
foreach($att as $name => $value){
90-
if(isset($this->optionValidator[$name]) && !$this->optionValidator[$name]->validate($value)){
89+
foreach ($att as $name => $value) {
90+
if (isset($this->optionValidator[$name]) && !$this->optionValidator[$name]->validate($value)) {
9191
return false;
9292
}
9393
}
@@ -127,12 +127,11 @@ public function asHtml(ElementNode $el)
127127

128128
if ($this->usesOption()) {
129129
$options = $el->getAttribute();
130-
if(count($options)==1){
130+
if (count($options)==1) {
131131
$vals = array_values($options);
132132
$html = str_ireplace('{option}', reset($vals), $html);
133-
}
134-
else{
135-
foreach($options as $key => $val){
133+
} else {
134+
foreach ($options as $key => $val) {
136135
$html = str_ireplace('{' . $key . '}', $val, $html);
137136
}
138137
}
@@ -145,15 +144,18 @@ public function asHtml(ElementNode $el)
145144
return $html;
146145
}
147146

148-
protected function getContent(ElementNode $el){
147+
protected function getContent(ElementNode $el)
148+
{
149149
if ($this->parseContent()) {
150150
$content = "";
151-
foreach ($el->getChildren() as $child)
151+
foreach ($el->getChildren() as $child) {
152152
$content .= $child->getAsHTML();
153+
}
153154
} else {
154155
$content = "";
155-
foreach ($el->getChildren() as $child)
156+
foreach ($el->getChildren() as $child) {
156157
$content .= $child->getAsBBCode();
158+
}
157159
}
158160
return $content;
159161
}
@@ -173,8 +175,9 @@ public function asText(ElementNode $el)
173175
}
174176

175177
$s = "";
176-
foreach ($el->getChildren() as $child)
178+
foreach ($el->getChildren() as $child) {
177179
$s .= $child->getAsText();
180+
}
178181
return $s;
179182
}
180183

JBBCode/CodeDefinitionBuilder.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function setParseContent($parseContent)
100100
*/
101101
public function setNestLimit($limit)
102102
{
103-
if(!is_int($limit) || ($limit <= 0 && -1 != $limit)) {
103+
if (!is_int($limit) || ($limit <= 0 && -1 != $limit)) {
104104
throw new \InvalidArgumentException("A nest limit must be a positive integer " .
105105
"or -1.");
106106
}
@@ -116,7 +116,7 @@ public function setNestLimit($limit)
116116
*/
117117
public function setOptionValidator(\JBBCode\InputValidator $validator, $option=null)
118118
{
119-
if(empty($option)){
119+
if (empty($option)) {
120120
$option = $this->tagName;
121121
}
122122
$this->optionValidator[$option] = $validator;
@@ -171,6 +171,4 @@ public function build()
171171
$this->bodyValidator);
172172
return $definition;
173173
}
174-
175-
176174
}

JBBCode/CodeDefinitionSet.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ interface CodeDefinitionSet
1919
* @return CodeDefinition[]
2020
*/
2121
public function getCodeDefinitions();
22-
2322
}

JBBCode/DefaultCodeDefinitionSet.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,4 @@ public function getCodeDefinitions()
7373
{
7474
return $this->definitions;
7575
}
76-
7776
}

JBBCode/DocumentElement.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct()
3232
public function getAsBBCode()
3333
{
3434
$s = "";
35-
foreach($this->getChildren() as $child){
35+
foreach ($this->getChildren() as $child) {
3636
$s .= $child->getAsBBCode();
3737
}
3838

@@ -52,8 +52,9 @@ public function getAsBBCode()
5252
public function getAsHTML()
5353
{
5454
$s = "";
55-
foreach($this->getChildren() as $child)
55+
foreach ($this->getChildren() as $child) {
5656
$s .= $child->getAsHTML();
57+
}
5758

5859
return $s;
5960
}
@@ -62,5 +63,4 @@ public function accept(NodeVisitor $visitor)
6263
{
6364
$visitor->visitDocumentElement($this);
6465
}
65-
6666
}

JBBCode/ElementNode.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ public function getAsText()
107107
return $this->codeDefinition->asText($this);
108108
} else {
109109
$s = "";
110-
foreach ($this->getChildren() as $child)
110+
foreach ($this->getChildren() as $child) {
111111
$s .= $child->getAsText();
112+
}
112113
return $s;
113114
}
114115
}
@@ -125,15 +126,14 @@ public function getAsBBCode()
125126
{
126127
$str = "[".$this->tagName;
127128
if (!empty($this->attribute)) {
128-
if(isset($this->attribute[$this->tagName])) {
129+
if (isset($this->attribute[$this->tagName])) {
129130
$str .= "=".$this->attribute[$this->tagName];
130131
}
131132

132-
foreach($this->attribute as $key => $value){
133-
if($key == $this->tagName){
133+
foreach ($this->attribute as $key => $value) {
134+
if ($key == $this->tagName) {
134135
continue;
135-
}
136-
else{
136+
} else {
137137
$str .= " ".$key."=" . $value;
138138
}
139139
}
@@ -157,7 +157,7 @@ public function getAsBBCode()
157157
*/
158158
public function getAsHTML()
159159
{
160-
if($this->codeDefinition) {
160+
if ($this->codeDefinition) {
161161
return $this->codeDefinition->asHtml($this);
162162
} else {
163163
return "";
@@ -185,8 +185,9 @@ public function addChild(Node $child)
185185
public function removeChild(Node $child)
186186
{
187187
foreach ($this->children as $key => $value) {
188-
if ($value == $child)
188+
if ($value == $child) {
189189
unset($this->children[$key]);
190+
}
190191
}
191192
}
192193

@@ -234,5 +235,4 @@ public function closestParentOfType($str)
234235
return $currentEl;
235236
}
236237
}
237-
238238
}

JBBCode/InputValidator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ interface InputValidator
1818
* @return boolean
1919
*/
2020
public function validate($input);
21-
2221
}

JBBCode/NodeVisitor.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010
*/
1111
interface NodeVisitor
1212
{
13-
1413
public function visitDocumentElement(DocumentElement $documentElement);
1514

1615
public function visitTextNode(TextNode $textNode);
1716

1817
public function visitElementNode(ElementNode $elementNode);
19-
2018
}

JBBCode/Parser.php

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
class Parser
2626
{
27-
2827
const OPTION_STATE_DEFAULT = 0;
2928
const OPTION_STATE_TAGNAME = 1;
3029
const OPTION_STATE_KEY = 2;
@@ -103,7 +102,8 @@ public function addCodeDefinition(CodeDefinition $definition)
103102
*
104103
* @return Parser
105104
*/
106-
public function addCodeDefinitionSet(CodeDefinitionSet $set) {
105+
public function addCodeDefinitionSet(CodeDefinitionSet $set)
106+
{
107107
foreach ($set->getCodeDefinitions() as $def) {
108108
$this->addCodeDefinition($def);
109109
}
@@ -233,7 +233,7 @@ public function codeExists($tagName, $usesOption = false)
233233
*/
234234
public function getCode($tagName, $usesOption = false)
235235
{
236-
if($this->codeExists($tagName, $usesOption)) {
236+
if ($this->codeExists($tagName, $usesOption)) {
237237
return $this->bbcodes[strtolower($tagName)][$usesOption];
238238
}
239239

@@ -297,8 +297,7 @@ protected function parseStartState(ElementNode $parent, Tokenizer $tokenizer)
297297

298298
if ('[' == $next) {
299299
return $this->parseTagOpen($parent, $tokenizer);
300-
}
301-
else {
300+
} else {
302301
$this->createTextNode($parent, $next);
303302
/* Drop back into the main parse loop which will call this
304303
* same method again. */
@@ -317,7 +316,6 @@ protected function parseStartState(ElementNode $parent, Tokenizer $tokenizer)
317316
*/
318317
protected function parseTagOpen(ElementNode $parent, Tokenizer $tokenizer)
319318
{
320-
321319
if (!$tokenizer->hasNext()) {
322320
/* The [ that sent us to this state was just a trailing [, not the
323321
* opening for a new tag. Treat it as such. */
@@ -350,8 +348,7 @@ protected function parseTagOpen(ElementNode $parent, Tokenizer $tokenizer)
350348
$after_next = $tokenizer->next();
351349
$tokenizer->stepBack();
352350

353-
if ($after_next != ']')
354-
{
351+
if ($after_next != ']') {
355352
$this->createTextNode($parent, '['.$next);
356353
return $parent;
357354
}
@@ -380,20 +377,20 @@ protected function parseOptions($tagContent)
380377
$done = false;
381378
$idx = 0;
382379

383-
try{
384-
while(!$done){
380+
try {
381+
while (!$done) {
385382
$char = $idx < $len ? $tagContent[$idx]:null;
386-
switch($state){
383+
switch ($state) {
387384
case static::OPTION_STATE_TAGNAME:
388-
switch($char){
385+
switch ($char) {
389386
case '=':
390387
$state = static::OPTION_STATE_VALUE;
391388
$tagName = $buffer;
392389
$keys[] = $tagName;
393390
$buffer = "";
394391
break;
395392
case ' ':
396-
if($buffer) {
393+
if ($buffer) {
397394
$state = static::OPTION_STATE_DEFAULT;
398395
$tagName = $buffer;
399396
$buffer = '';
@@ -415,7 +412,7 @@ protected function parseOptions($tagContent)
415412
break;
416413

417414
case static::OPTION_STATE_DEFAULT:
418-
switch($char){
415+
switch ($char) {
419416
case ' ':
420417
// do nothing
421418
default:
@@ -425,7 +422,7 @@ protected function parseOptions($tagContent)
425422
break;
426423

427424
case static::OPTION_STATE_VALUE:
428-
switch($char){
425+
switch ($char) {
429426
case '"':
430427
$state = static::OPTION_STATE_QUOTED_VALUE;
431428
break;
@@ -436,7 +433,7 @@ protected function parseOptions($tagContent)
436433
$state = static::OPTION_STATE_KEY;
437434
break;
438435
case ":":
439-
if($buffer=="javascript"){
436+
if ($buffer=="javascript") {
440437
$state = static::OPTION_STATE_JAVASCRIPT;
441438
}
442439
$buffer .= $char;
@@ -448,7 +445,7 @@ protected function parseOptions($tagContent)
448445
break;
449446

450447
case static::OPTION_STATE_JAVASCRIPT:
451-
switch($char){
448+
switch ($char) {
452449
case ";":
453450
$buffer .= $char;
454451
$values[] = $buffer;
@@ -462,7 +459,7 @@ protected function parseOptions($tagContent)
462459
break;
463460

464461
case static::OPTION_STATE_KEY:
465-
switch($char){
462+
switch ($char) {
466463
case '=':
467464
$state = static::OPTION_STATE_VALUE;
468465
$keys[] = trim($buffer);
@@ -477,15 +474,15 @@ protected function parseOptions($tagContent)
477474
break;
478475

479476
case static::OPTION_STATE_QUOTED_VALUE:
480-
switch($char){
477+
switch ($char) {
481478
case null:
482479
case '"':
483480
$state = static::OPTION_STATE_KEY;
484481
$values[] = $buffer;
485482
$buffer = '';
486483

487484
// peek ahead. If the next character is not a space or a closing brace, we have a bad tag and need to abort
488-
if(isset($tagContent[$idx+1]) && $tagContent[$idx+1]!=" " && $tagContent[$idx+1]!="]" ){
485+
if (isset($tagContent[$idx+1]) && $tagContent[$idx+1]!=" " && $tagContent[$idx+1]!="]") {
489486
throw new \DomainException("Badly formed attribute: $tagContent");
490487
}
491488
break;
@@ -495,26 +492,25 @@ protected function parseOptions($tagContent)
495492
}
496493
break;
497494
default:
498-
if(!empty($char)){
495+
if (!empty($char)) {
499496
$state = static::OPTION_STATE_KEY;
500497
}
501498

502499
}
503-
if($idx >= $len){
500+
if ($idx >= $len) {
504501
$done = true;
505502
}
506503
$idx++;
507504
}
508505

509-
if(!empty($keys) && !empty($values)){
510-
if(count($keys)==(count($values)+1)){
506+
if (!empty($keys) && !empty($values)) {
507+
if (count($keys)==(count($values)+1)) {
511508
array_unshift($values, "");
512509
}
513510

514511
$options = array_combine($keys, $values);
515512
}
516-
}
517-
catch(\DomainException $e){
513+
} catch (\DomainException $e) {
518514
// if we're in this state, then something evidently went wrong. We'll consider everything that came after the tagname to be the attribute for that keyname
519515
$options[$tagName]= substr($tagContent, strpos($tagContent, "=")+1);
520516
}
@@ -641,5 +637,4 @@ protected function parseAsTextUntilClose(ElementNode $parent, Tokenizer $tokeniz
641637
$curr = $tokenizer->next();
642638
}
643639
}
644-
645640
}

JBBCode/TextNode.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,4 @@ public function setValue($newValue)
9898
{
9999
$this->value = $newValue;
100100
}
101-
102101
}

0 commit comments

Comments
 (0)