Skip to content

Commit 8734e26

Browse files
committed
Code style
1 parent 414010f commit 8734e26

2 files changed

Lines changed: 90 additions & 47 deletions

File tree

Parserus.php

Lines changed: 87 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function addBBCode(array $bb): self
159159
'self_nesting' => false,
160160
];
161161

162-
if ($bb['tag'] === 'ROOT') {
162+
if ('ROOT' === $bb['tag']) {
163163
$tag = 'ROOT';
164164
} else {
165165
$tag = strtolower($bb['tag']);
@@ -168,7 +168,7 @@ public function addBBCode(array $bb): self
168168
if (isset($bb['type'])) {
169169
$res['type'] = $bb['type'];
170170

171-
if ($bb['type'] !== 'inline') {
171+
if ('inline' !== $bb['type']) {
172172
$res['parents'] = ['block' => 1];
173173
$res['auto'] = false;
174174
}
@@ -240,7 +240,10 @@ public function addBBCode(array $bb): self
240240
$required[] = $attr;
241241
}
242242

243-
if ($attr !== 'Def' && $attr !== 'No_attr') {
243+
if (
244+
'Def' !== $attr
245+
&& 'No_attr' !== $attr
246+
) {
244247
$other = true;
245248
}
246249
}
@@ -328,7 +331,7 @@ protected function createSmPattern(): void
328331
if (count($sub) > 1) {
329332
$pattern .= $quote . preg_quote($symbol, '%') . '(?:' . implode('|', $sub) . ')';
330333
$quote = '|';
331-
} else if (count($sub) == 1) {
334+
} elseif (1 == count($sub)) {
332335
$pattern .= $quote . preg_quote($symbol, '%') . $sub[0];
333336
$quote = '|';
334337
}
@@ -488,14 +491,18 @@ protected function addTextNode(string $text, int $parentId): string
488491
protected function getNormAttr(string $attr): string
489492
{
490493
// удаление крайних кавычек
491-
if (isset($attr[1])
492-
&& $attr[0] === $attr[strlen($attr) - 1]
493-
&& ($attr[0] === '"' || $attr[0] === '\'')
494+
if (
495+
isset($attr[1])
496+
&& $attr[0] === $attr[-1]
497+
&& (
498+
'"' === $attr[0]
499+
|| '\'' === $attr[0]
500+
)
494501
) {
495502
return substr($attr, 1, -1);
503+
} else {
504+
return $attr;
496505
}
497-
498-
return $attr;
499506
}
500507

501508
/**
@@ -512,7 +519,7 @@ protected function parseAttrs(string $tag, string $type, string $text): ?array
512519
$attrs = [];
513520
$tagText = '';
514521

515-
if ($type === '=') {
522+
if ('=' === $type) {
516523
$pattern = '%^(?!\x20)
517524
("[^\x00-\x1f"]*(?:"+(?!\x20*+\]|\x20++[a-z-]{2,15}=)[^\x00-\x1f"]*)*"
518525
|\'[^\x00-\x1f\']*(?:\'+(?!\x20*+\]|\x20++[a-z-]{2,15}=)[^\x00-\x1f\']*)*\'
@@ -535,7 +542,8 @@ protected function parseAttrs(string $tag, string $type, string $text): ?array
535542
$attrs['Def'] = $tmp;
536543

537544
// в теге не может быть первичного атрибута
538-
if ($this->strict
545+
if (
546+
$this->strict
539547
&& ! isset($this->bbcodes[$tag]['attrs']['Def'])
540548
) {
541549
$this->errors[] = [7, $tag];
@@ -545,7 +553,7 @@ protected function parseAttrs(string $tag, string $type, string $text): ?array
545553
}
546554
}
547555

548-
if ($type !== ']') {
556+
if (']' !== $type) {
549557
$pattern = '%^\x20*+([a-z-]{2,15})
550558
=(?!\x20)
551559
("[^\x00-\x1f"]*(?:"+(?!\x20*+\]|\x20++[a-z-]{2,15}=)[^\x00-\x1f"]*)*"
@@ -584,12 +592,13 @@ protected function parseAttrs(string $tag, string $type, string $text): ?array
584592
}
585593
}
586594

587-
} while ($match[3] !== ']');
595+
} while (']' !== $match[3]);
588596
}
589597

590598
if (empty($attrs)) {
591599
// в теге должны быть атрибуты
592-
if (! empty($this->bbcodes[$tag]['required'])
600+
if (
601+
! empty($this->bbcodes[$tag]['required'])
593602
|| ! isset($this->bbcodes[$tag]['attrs']['No_attr'])
594603
) {
595604
$this->errors[] = [6, $tag];
@@ -643,8 +652,9 @@ protected function findParent(string $tag)
643652
while (null !== $curId) {
644653
if (isset($this->bbcodes[$tag]['parents'][$this->bbcodes[$curTag]['type']])) {
645654
return $curId;
646-
} else if ($this->bbcodes[$tag]['type'] === 'inline'
647-
|| false === $this->bbcodes[$curTag]['auto']
655+
} elseif (
656+
'inline' === $this->bbcodes[$tag]['type']
657+
|| false === $this->bbcodes[$curTag]['auto']
648658
) {
649659
// тег не может быть открыт на этой позиции
650660
$this->errors[] = [3, $tag, $this->data[$this->curId]['tag']];
@@ -684,7 +694,8 @@ protected function validationTag(string $tag, array $attrs, string $text)
684694

685695
foreach ($attrs as $key => $val) {
686696
// проверка формата атрибута
687-
if (isset($bb['attrs'][$key]['format'])
697+
if (
698+
isset($bb['attrs'][$key]['format'])
688699
&& ! preg_match($bb['attrs'][$key]['format'], $val)
689700
) {
690701
$this->errors[] = [9, $tag, $key];
@@ -698,9 +709,12 @@ protected function validationTag(string $tag, array $attrs, string $text)
698709
}
699710

700711
// тело тега
701-
if (null === $body
702-
&& (isset($bb['attrs'][$key]['body_format'])
703-
|| isset($bb['attrs'][$key]['text_only']))
712+
if (
713+
null === $body
714+
&& (
715+
isset($bb['attrs'][$key]['body_format'])
716+
|| isset($bb['attrs'][$key]['text_only'])
717+
)
704718
) {
705719
$ptag = preg_quote($tag, '%');
706720
$match = preg_split('%^([^\[]*(?:\[(?!/' . $ptag . '\])[^\[]*)*)(?:\[/' . $ptag . '\])?%i', $text, 2, PREG_SPLIT_DELIM_CAPTURE);
@@ -718,7 +732,7 @@ protected function validationTag(string $tag, array $attrs, string $text)
718732
if (isset($bb['attrs'][$key]['body_format'])) {
719733
if (isset($tested[$bb['attrs'][$key]['body_format']])) {
720734
continue;
721-
} else if (! preg_match($bb['attrs'][$key]['body_format'], $body)) {
735+
} elseif (! preg_match($bb['attrs'][$key]['body_format'], $body)) {
722736
$this->errors[] = [11, $tag];
723737

724738
return false;
@@ -758,8 +772,12 @@ protected function closeTag(string $tag, string $curText, string $tagText): stri
758772
$curId = $this->curId;
759773
$curTag = $this->data[$curId]['tag'];
760774

761-
while ($curTag !== $tag && $curId > 0) {
762-
if ($this->bbcodes[$tag]['type'] === 'inline'
775+
while (
776+
$curTag !== $tag
777+
&& $curId > 0
778+
) {
779+
if (
780+
'inline' === $this->bbcodes[$tag]['type']
763781
|| false === $this->bbcodes[$curTag]['auto']
764782
) {
765783
break;
@@ -794,7 +812,7 @@ protected function reset(array $opts): void
794812
$this->data = [];
795813
$this->dataId = -1;
796814
$this->curId = $this->addTagNode(
797-
isset($opts['root']) && isset($this->bbcodes[$opts['root']])
815+
isset($opts['root'], $this->bbcodes[$opts['root']])
798816
? $opts['root']
799817
: 'ROOT'
800818
);
@@ -821,8 +839,9 @@ public function parse(string $text, array $opts = []): self
821839
$text = str_replace("\r\n", "\n", $text);
822840
$text = str_replace("\r", "\n", $text);
823841

824-
while (($match = preg_split('%(\[(/)?(' . ($recCount ? $recTag : '[a-z\*][a-z\d-]{0,10}') . ')((?(1)\]|[=\]\x20])))%i', $text, 2, PREG_SPLIT_DELIM_CAPTURE))
825-
&& isset($match[1])
842+
while (
843+
($match = preg_split('%(\[(/)?(' . ($recCount ? $recTag : '[a-z\*][a-z\d-]{0,10}') . ')((?(1)\]|[=\]\x20])))%i', $text, 2, PREG_SPLIT_DELIM_CAPTURE))
844+
&& isset($match[1])
826845
) {
827846
/* $match[0] - текст до тега
828847
* $match[1] - [ + (|/) + имя тега + (]| |=)
@@ -842,7 +861,10 @@ public function parse(string $text, array $opts = []): self
842861
}
843862

844863
if (! empty($match[2])) {
845-
if ($recCount && --$recCount) {
864+
if (
865+
$recCount
866+
&& --$recCount
867+
) {
846868
$curText .= $tagText;
847869
} else {
848870
$curText = $this->closeTag($tag, $curText, $tagText);
@@ -868,13 +890,19 @@ public function parse(string $text, array $opts = []): self
868890
continue;
869891
}
870892

871-
if (null !== $this->blackList && in_array($tag, $this->blackList)) {
893+
if (
894+
null !== $this->blackList
895+
&& in_array($tag, $this->blackList)
896+
) {
872897
$curText .= $tagText;
873898
$this->errors[] = [1, $tag];
874899
continue;
875900
}
876901

877-
if (null !== $this->whiteList && ! in_array($tag, $this->whiteList)) {
902+
if (
903+
null !== $this->whiteList
904+
&& ! in_array($tag, $this->whiteList)
905+
) {
878906
$curText .= $tagText;
879907
$this->errors[] = [2, $tag];
880908
continue;
@@ -905,7 +933,7 @@ public function parse(string $text, array $opts = []): self
905933
$text = $attrs['end'];
906934
$this->curId = $parentId;
907935

908-
} else if (isset($this->bbcodes[$tag]['single'])) {
936+
} elseif (isset($this->bbcodes[$tag]['single'])) {
909937
$this->curId = $parentId;
910938

911939
} else {
@@ -1012,8 +1040,9 @@ public function getHtml(int $id = 0): string
10121040
return '';
10131041
}
10141042

1015-
switch (2 * (end($this->data[$pid]['children']) === $id)
1016-
+ ($this->data[$pid]['children'][0] === $id)
1043+
switch (
1044+
2 * (end($this->data[$pid]['children']) === $id)
1045+
+ ($this->data[$pid]['children'][0] === $id)
10171046
) {
10181047
case 1:
10191048
$text = $this->e(preg_replace('%^\x20*\n%', '', $this->data[$id]['text']));
@@ -1029,7 +1058,8 @@ public function getHtml(int $id = 0): string
10291058
break;
10301059
}
10311060

1032-
if (empty($this->data[$pid]['text_only'])
1061+
if (
1062+
empty($this->data[$pid]['text_only'])
10331063
&& $this->smOn
10341064
&& isset($this->bbcodes[$this->smTag]['parents'][$bb['type']])
10351065
&& ! isset($this->smBL[$this->data[$pid]['tag']])
@@ -1068,7 +1098,7 @@ public function getCode(int $id = 0): string
10681098
$body .= $this->getCode($cid);
10691099
}
10701100

1071-
if ($id === 0) {
1101+
if (0 === $id) {
10721102
return $body;
10731103
}
10741104

@@ -1080,13 +1110,19 @@ public function getCode(int $id = 0): string
10801110
$count = count($attrs);
10811111
foreach ($attrs as $attr => $val) {
10821112
$quote = '';
1083-
if ($count > 1 || strpbrk($val, ' \'"]')) {
1113+
if (
1114+
$count > 1
1115+
|| strpbrk($val, ' \'"]')
1116+
) {
10841117
$quote = '"';
1085-
if (false !== strpos($val, '"') && false === strpos($val, '\'')) {
1118+
if (
1119+
false !== strpos($val, '"')
1120+
&& false === strpos($val, '\'')
1121+
) {
10861122
$quote = '\'';
10871123
}
10881124
}
1089-
if ($attr === 'Def') {
1125+
if ('Def' === $attr) {
10901126
$def = '=' . $quote . $val . $quote;
10911127
} else {
10921128
$other .= ' ' . $attr . '=' . $quote . $val . $quote;
@@ -1177,9 +1213,15 @@ protected function detect(string $tag, string $pattern, bool $textOnly): self
11771213
}
11781214

11791215
$error = null;
1180-
if (null !== $this->blackList && in_array($tag, $this->blackList)) {
1216+
if (
1217+
null !== $this->blackList
1218+
&& in_array($tag, $this->blackList)
1219+
) {
11811220
$error = 1;
1182-
} else if (null !== $this->whiteList && ! in_array($tag, $this->whiteList)) {
1221+
} elseif (
1222+
null !== $this->whiteList
1223+
&& ! in_array($tag, $this->whiteList)
1224+
) {
11831225
$error = 2;
11841226
}
11851227

@@ -1192,15 +1234,16 @@ protected function detect(string $tag, string $pattern, bool $textOnly): self
11921234
$pid = $this->data[$id]['parent'];
11931235

11941236
// родитель может содержать только текст или не подходит по типу
1195-
if (isset($this->data[$pid]['text_only']) ||
1196-
! isset($this->bbcodes[$tag]['parents'][$this->bbcodes[$this->data[$pid]['tag']]['type']])
1237+
if (
1238+
isset($this->data[$pid]['text_only'])
1239+
|| ! isset($this->bbcodes[$tag]['parents'][$this->bbcodes[$this->data[$pid]['tag']]['type']])
11971240
) {
11981241
continue;
11991242
}
12001243

12011244
if (! preg_match_all($pattern, $this->data[$id]['text'], $matches, PREG_OFFSET_CAPTURE)) {
12021245
continue;
1203-
} else if ($error) {
1246+
} elseif ($error) {
12041247
$this->errors[] = [$error, $tag];
12051248

12061249
return $this;
@@ -1269,7 +1312,7 @@ protected function stripEmptyTags_(string $mask, int $id): bool
12691312
// текстовый узел
12701313
if (isset($this->data[$id]['text'])) {
12711314
if (isset($mask[0])) {
1272-
return trim($this->data[$id]['text'], $mask) === '';
1315+
return '' === trim($this->data[$id]['text'], $mask);
12731316
} else {
12741317
return false;
12751318
}
@@ -1337,7 +1380,7 @@ public function getErrors(array $lang = [], array $errors = []): array
13371380

13381381
if (isset($lang[$err])) {
13391382
$text = $lang[$err];
1340-
} else if (isset($defLang[$err])) {
1383+
} elseif (isset($defLang[$err])) {
13411384
$text = $defLang[$err];
13421385
} else {
13431386
$text = 'Unknown error';

examples/bbcodes_test.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,11 @@
355355

356356
if (strpos($url, 'www.') === 0) {
357357
$fUrl = 'http://'.$fUrl;
358-
} else if (strpos($url, 'ftp.') === 0) {
358+
} elseif (strpos($url, 'ftp.') === 0) {
359359
$fUrl = 'ftp://'.$fUrl;
360-
} else if (strpos($url, '/') === 0) {
360+
} elseif (strpos($url, '/') === 0) {
361361
$fUrl = $parser->attr('baseUrl') . $fUrl;
362-
} else if (!preg_match('%^([a-z0-9]{3,6})://%', $url)) {
362+
} elseif (!preg_match('%^([a-z0-9]{3,6})://%', $url)) {
363363
$fUrl = 'http://'.$fUrl;
364364
}
365365

0 commit comments

Comments
 (0)