Skip to content

Commit 3f46a8a

Browse files
committed
Various code improvements/fixes
- Fix "Creation of dynamic property" warnings - Fix a couple of code errors found in static code analisys - Enable testing on PHP 8.2 and 8.3
1 parent 7b48811 commit 3f46a8a

File tree

11 files changed

+70
-22
lines changed

11 files changed

+70
-22
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.gitattributes export-ignore
33
.github/ export-ignore
44
tests/ export-ignore
5+
phpstan.neon export-ignore

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
fail-fast: true
1414
matrix:
15-
php: [5.4, 5.5, 5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1]
15+
php: [5.4, 5.5, 5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2, 8.3]
1616

1717
name: PHP ${{ matrix.php }}/Linux
1818

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 2.1
4+
5+
- Fix "Creation of dynamic property" warnings
6+
- Fix a couple of code errors found in static code analisys
7+
- Enable testing on PHP 8.2 and 8.3
8+
39
## 2.0
410

511
- Fork, various code style fixes, camel-case use

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
}
3131
},
3232
"require-dev": {
33-
"phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6 || ^7"
33+
"phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6 || ^7 | ^9.6",
34+
"phpstan/phpstan": "^1.2"
3435
}
3536
}

phpstan.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 5
3+
paths:
4+
- src
5+
- tests

src/Document.php

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

55
class Document
66
{
7+
/** @var ?string Current character in an RTF stream */
8+
protected $char;
79
/** @var string RTF string being parsed */
810
protected $rtf;
911
/** @var int Current position in RTF string */
1012
protected $pos;
1113
/** @var int Length of RTF string */
1214
protected $len;
13-
/** @var Group Current RTF group */
15+
/** @var ?Group Current RTF group */
1416
protected $group;
17+
/** @var array */
18+
protected $uc = [];
1519

16-
/** @var Group Root group */
20+
/** @var ?Group Root group */
1721
public $root = null;
1822

1923
/**
@@ -27,10 +31,10 @@ public function __construct($rtf)
2731
}
2832

2933
/**
30-
* Get the next character from the RTF stream.
34+
* Position on the next character from the RTF stream.
3135
* Parsing is aborted when reading beyond end of input string.
3236
*
33-
* @return string
37+
* @return void
3438
*/
3539
protected function getChar()
3640
{
@@ -110,7 +114,7 @@ protected function parseStartGroup()
110114
{
111115
$group = new Group();
112116

113-
if ($this->group != null) {
117+
if ($this->group) {
114118
// Make the new group a child of the current group
115119
$group->parent = $this->group;
116120

@@ -170,7 +174,7 @@ protected function parseControlWord()
170174
if ($parameter === null) {
171175
$parameter = 0;
172176
}
173-
$parameter = $parameter * 10 + $this->char;
177+
$parameter = $parameter * 10 + (int) $this->char;
174178
$this->getChar();
175179
}
176180

@@ -349,7 +353,7 @@ protected function parseText()
349353

350354
// If there is no current group, then this is not a valid RTF file.
351355
// Throw an exception.
352-
if ($this->group == null) {
356+
if (!$this->group) {
353357
throw new \Exception("Parse error: RTF text outside of group.");
354358
}
355359

src/Html/Font.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
class Font
66
{
77
public $family;
8+
public $fprq;
89
public $name;
910
public $charset;
1011
public $codepage;

src/Html/HtmlFormatter.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66

77
class HtmlFormatter
88
{
9-
protected $output = '';
109
protected $encoding;
1110
protected $defaultFont;
1211
protected $fromhtml = false;
12+
protected $openedTags = [];
13+
protected $output = '';
14+
protected $previousState;
15+
protected $rtfEncoding;
16+
protected $state;
17+
protected $states = [];
1318

1419
/**
1520
* Object constructor.
@@ -65,8 +70,8 @@ public function format(Document $document)
6570
$this->processGroup($document->root);
6671

6772
// Instead of removing opened tags, we close them
68-
$this->output .= $this->openedTags['span'] ? '</span>' : '';
69-
$this->output .= $this->openedTags['p'] ? '</p>' : '';
73+
$this->output .= $this->openedTags['span'] ? '</span>' : ''; // @phpstan-ignore-line
74+
$this->output .= $this->openedTags['p'] ? '</p>' : ''; // @phpstan-ignore-line
7075

7176
// Remove extra empty paragraph at the end
7277
// TODO: Find the real reason it's there and fix it
@@ -490,7 +495,7 @@ protected function write($txt)
490495
return;
491496
}
492497

493-
if ($this->openedTags['p'] === null) {
498+
if (!isset($this->openedTags['p'])) {
494499
// Create the first paragraph
495500
$this->openTag('p');
496501
}
@@ -499,9 +504,7 @@ protected function write($txt)
499504
// 1st case: style change occured
500505
// 2nd case: there is no change in style but the already created 'span'
501506
// element is somehow closed (ex. because of an end of paragraph)
502-
if (!$this->state->equals($this->previousState)
503-
|| ($this->state->equals($this->previousState) && !$this->openedTags['span'])
504-
) {
507+
if (!$this->state->equals($this->previousState) || empty($this->openedTags['span'])) {
505508
// If applicable close previously opened 'span' tag
506509
$this->closeTag('span');
507510

@@ -535,7 +538,7 @@ protected function closeTag($tag)
535538
return;
536539
}
537540

538-
if ($this->openedTags[$tag]) {
541+
if (!empty($this->openedTags[$tag])) {
539542
// Check for empty html elements
540543
if (substr($this->output, -strlen("<{$tag}>")) == "<{$tag}>") {
541544
switch ($tag) {
@@ -649,6 +652,8 @@ protected function getEncodingFromCharset($charset)
649652
if (isset($map[$charset])) {
650653
return $map[$charset];
651654
}
655+
656+
return null;
652657
}
653658

654659
/**
@@ -702,12 +707,14 @@ protected function getEncodingFromCodepage($cpg)
702707
if (isset($map[$cpg])) {
703708
return $map[$cpg];
704709
}
710+
711+
return null;
705712
}
706713

707714
protected function ordUtf8($chr)
708715
{
709716
$ord0 = ord($chr);
710-
if ($ord0 >= 0 && $ord0 <= 127) {
717+
if ($ord0 <= 127) {
711718
return $ord0;
712719
}
713720

src/Html/Image.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44

55
class Image
66
{
7+
public $format;
8+
public $width;
9+
public $height;
10+
public $goalWidth;
11+
public $goalHeight;
12+
public $pcScaleX;
13+
public $pcScaleY;
14+
public $binarySize;
15+
public $imageData;
16+
17+
718
/**
819
* Object constructor.
920
*/
@@ -33,7 +44,7 @@ public function reset()
3344
/**
3445
* Generate a HTML content for the image
3546
*
36-
* @return string <img> tag content, An empty string for unsupported/empty image
47+
* @return string Image tag content, An empty string for unsupported/empty image
3748
*/
3849
public function printImage()
3950
{

src/Html/State.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ class State
66
{
77
public static $fonttbl = [];
88
public static $colortbl = [];
9+
public $bold;
10+
public $italic;
11+
public $underline;
12+
public $strike;
13+
public $hidden;
14+
public $fontsize;
15+
public $fontcolor;
16+
public $background;
17+
public $hcolor;
18+
public $font;
19+
public $htmlrtf;
920

1021
protected static $highlight = [
1122
1 => 'Black',
@@ -26,6 +37,7 @@ class State
2637
16 => 'LightGray'
2738
];
2839

40+
2941
/**
3042
* Object constructor
3143
*/
@@ -94,11 +106,11 @@ public function printStyle()
94106
// a dedicated state->end_underline variable
95107
// if($this->state->end_underline) {$span .= "text-decoration:none";}
96108
if ($this->strike) {
97-
$style .= "text-decoration:line-through";
109+
$style[] = "text-decoration:line-through";
98110
}
99111

100112
if ($this->hidden) {
101-
$style .= "display:none";
113+
$style[] = "display:none";
102114
}
103115

104116
if (isset($this->font)) {

0 commit comments

Comments
 (0)