Skip to content

Commit

Permalink
Various code improvements/fixes
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
alecpl committed Dec 31, 2023
1 parent 7b48811 commit 3f46a8a
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.gitattributes export-ignore
.github/ export-ignore
tests/ export-ignore
phpstan.neon export-ignore
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [5.4, 5.5, 5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1]
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]

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

Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 2.1

- 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

## 2.0

- Fork, various code style fixes, camel-case use
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6 || ^7"
"phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6 || ^7 | ^9.6",
"phpstan/phpstan": "^1.2"
}
}
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 5
paths:
- src
- tests
18 changes: 11 additions & 7 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

class Document
{
/** @var ?string Current character in an RTF stream */
protected $char;
/** @var string RTF string being parsed */
protected $rtf;
/** @var int Current position in RTF string */
protected $pos;
/** @var int Length of RTF string */
protected $len;
/** @var Group Current RTF group */
/** @var ?Group Current RTF group */
protected $group;
/** @var array */
protected $uc = [];

/** @var Group Root group */
/** @var ?Group Root group */
public $root = null;

/**
Expand All @@ -27,10 +31,10 @@ public function __construct($rtf)
}

/**
* Get the next character from the RTF stream.
* Position on the next character from the RTF stream.
* Parsing is aborted when reading beyond end of input string.
*
* @return string
* @return void
*/
protected function getChar()
{
Expand Down Expand Up @@ -110,7 +114,7 @@ protected function parseStartGroup()
{
$group = new Group();

if ($this->group != null) {
if ($this->group) {
// Make the new group a child of the current group
$group->parent = $this->group;

Expand Down Expand Up @@ -170,7 +174,7 @@ protected function parseControlWord()
if ($parameter === null) {
$parameter = 0;
}
$parameter = $parameter * 10 + $this->char;
$parameter = $parameter * 10 + (int) $this->char;
$this->getChar();
}

Expand Down Expand Up @@ -349,7 +353,7 @@ protected function parseText()

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

Expand Down
1 change: 1 addition & 0 deletions src/Html/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Font
{
public $family;
public $fprq;
public $name;
public $charset;
public $codepage;
Expand Down
25 changes: 16 additions & 9 deletions src/Html/HtmlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@

class HtmlFormatter
{
protected $output = '';
protected $encoding;
protected $defaultFont;
protected $fromhtml = false;
protected $openedTags = [];
protected $output = '';
protected $previousState;
protected $rtfEncoding;
protected $state;
protected $states = [];

/**
* Object constructor.
Expand Down Expand Up @@ -65,8 +70,8 @@ public function format(Document $document)
$this->processGroup($document->root);

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

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

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

Expand Down Expand Up @@ -535,7 +538,7 @@ protected function closeTag($tag)
return;
}

if ($this->openedTags[$tag]) {
if (!empty($this->openedTags[$tag])) {
// Check for empty html elements
if (substr($this->output, -strlen("<{$tag}>")) == "<{$tag}>") {
switch ($tag) {
Expand Down Expand Up @@ -649,6 +652,8 @@ protected function getEncodingFromCharset($charset)
if (isset($map[$charset])) {
return $map[$charset];
}

return null;
}

/**
Expand Down Expand Up @@ -702,12 +707,14 @@ protected function getEncodingFromCodepage($cpg)
if (isset($map[$cpg])) {
return $map[$cpg];
}

return null;
}

protected function ordUtf8($chr)
{
$ord0 = ord($chr);
if ($ord0 >= 0 && $ord0 <= 127) {
if ($ord0 <= 127) {
return $ord0;
}

Expand Down
13 changes: 12 additions & 1 deletion src/Html/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

class Image
{
public $format;
public $width;
public $height;
public $goalWidth;
public $goalHeight;
public $pcScaleX;
public $pcScaleY;
public $binarySize;
public $imageData;


/**
* Object constructor.
*/
Expand Down Expand Up @@ -33,7 +44,7 @@ public function reset()
/**
* Generate a HTML content for the image
*
* @return string <img> tag content, An empty string for unsupported/empty image
* @return string Image tag content, An empty string for unsupported/empty image
*/
public function printImage()
{
Expand Down
16 changes: 14 additions & 2 deletions src/Html/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ class State
{
public static $fonttbl = [];
public static $colortbl = [];
public $bold;
public $italic;
public $underline;
public $strike;
public $hidden;
public $fontsize;
public $fontcolor;
public $background;
public $hcolor;
public $font;
public $htmlrtf;

protected static $highlight = [
1 => 'Black',
Expand All @@ -26,6 +37,7 @@ class State
16 => 'LightGray'
];


/**
* Object constructor
*/
Expand Down Expand Up @@ -94,11 +106,11 @@ public function printStyle()
// a dedicated state->end_underline variable
// if($this->state->end_underline) {$span .= "text-decoration:none";}
if ($this->strike) {
$style .= "text-decoration:line-through";
$style[] = "text-decoration:line-through";
}

if ($this->hidden) {
$style .= "display:none";
$style[] = "display:none";
}

if (isset($this->font)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/FontFamilyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use RtfHtmlPhp\Document;
use RtfHtmlPhp\Html\HtmlFormatter;

class FontFamilyTestTest extends TestCase
class FontFamilyTest extends TestCase
{
public function testParseFontFamilyHtml()
{
Expand Down

0 comments on commit 3f46a8a

Please sign in to comment.