Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements #147

Closed
wants to merge 7 commits into from
Prev Previous commit
Next Next commit
Improved performance: made in_array calls on strings strict, extracte…
…d repeatedly indentical declared parameters in reused variable
  • Loading branch information
Kay Stenschke committed Nov 30, 2016
commit 9675866e7af5d31ef95682853779f6876ea282f1
8 changes: 2 additions & 6 deletions src/CssConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ protected function convertSubColor($c)
*/
public function convertBackground($css, &$value)
{
// is there a image ?
// is there an image ?
$text = '/url\(([^)]*)\)/isU';
if (preg_match($text, $css, $match)) {
// get the image
Expand Down Expand Up @@ -249,11 +249,7 @@ public function convertBackground($css, &$value)
public function convertBackgroundColor($css)
{
$res = null;
if ($css === 'transparent') {
return null;
} else {
return $this->convertToColor($css, $res);
}
return $css === 'transparent' ? null : $this->convertToColor($css, $res);
}

/**
Expand Down
35 changes: 18 additions & 17 deletions src/Html2Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public function Output($name = '', $dest = false)

// clean up the destination
$dest = strtoupper($dest);
if (!in_array($dest, array('I', 'D', 'F', 'S', 'FI','FD'))) {
if (!in_array($dest, array('I', 'D', 'F', 'S', 'FI','FD'), true)) {
$dest = 'I';
}

Expand Down Expand Up @@ -1201,10 +1201,10 @@ protected function _listeAddLevel($type = 'ul', $style = '', $img = null, $start
}

// prepare the datas
if (!in_array($type, array('ul', 'ol'))) {
if (!in_array($type, array('ul', 'ol'), true)) {
$type = 'ul';
}
if (!in_array($style, array('lower-alpha', 'upper-alpha', 'upper-roman', 'lower-roman', 'decimal', 'square', 'circle', 'disc', 'none'))) {
if (!in_array($style, array('lower-alpha', 'upper-alpha', 'upper-roman', 'lower-roman', 'decimal', 'square', 'circle', 'disc', 'none'), true)) {
$style = '';
}

Expand Down Expand Up @@ -1257,7 +1257,7 @@ protected function _makeHTMLcode()
$actionName = $action->getName();

// if it is a opening of table / ul / ol
if (in_array($actionName, $tagsTableUlOl) && !$action->isClose()) {
if (in_array($actionName, $tagsTableUlOl, true) && !$action->isClose()) {
// we will work as a sub HTML to calculate the size of the element
$this->_subPart = true;

Expand Down Expand Up @@ -2668,10 +2668,10 @@ protected function _tag_open_PAGE($param)
foreach ($lst as $key => $val) {
$lst[$key] = trim(strtolower($val));
}
$page = in_array('page', $lst);
$date = in_array('date', $lst);
$hour = in_array('heure', $lst);
$form = in_array('form', $lst);
$page = in_array('page', $lst, true);
$date = in_array('date', $lst, true);
$hour = in_array('heure', $lst, true);
$form = in_array('form', $lst, true);
} else {
$page = null;
$date = null;
Expand Down Expand Up @@ -3020,7 +3020,7 @@ protected function _tag_open_DIV($param, $other = 'div')
$this->parsingCss->fontSet();

// for fieldset and legend
if (in_array($other, array('fieldset', 'legend'))) {
if (in_array($other, array('fieldset', 'legend'), true)) {
if (isset($param['moveTop'])) {
$this->parsingCss->value['margin']['t'] += $param['moveTop'];
}
Expand Down Expand Up @@ -3564,7 +3564,7 @@ protected function _tag_open_QRCODE($param)
if ($param['value'] === '') {
return true;
}
if (!in_array($param['ec'], array('L', 'M', 'Q', 'H'))) {
if (!in_array($param['ec'], array('L', 'M', 'Q', 'H'), true)) {
$param['ec'] = 'H';
}

Expand Down Expand Up @@ -3635,7 +3635,7 @@ protected function _tag_close_QRCODE($param)
protected function _tag_open_WRITE($param)
{
$fill = ($this->parsingCss->value['background']['color'] !== null && $this->parsingCss->value['background']['image'] === null);
if (in_array($this->parsingCss->value['id_tag'], array('fieldset', 'legend', 'div', 'table', 'tr', 'td', 'th'))) {
if (in_array($this->parsingCss->value['id_tag'], array('fieldset', 'legend', 'div', 'table', 'tr', 'td', 'th'), true)) {
$fill = false;
}

Expand Down Expand Up @@ -4172,7 +4172,7 @@ protected function _tag_open_P($param)
return false;
}

if (!in_array($this->_previousCall, array('_tag_close_P', '_tag_close_UL'))) {
if (!in_array($this->_previousCall, array('_tag_close_P', '_tag_close_UL'), true)) {
if ($this->_maxH) {
$this->_tag_open_BR(array());
}
Expand Down Expand Up @@ -4319,7 +4319,7 @@ protected function _tag_open_UL($param, $other = 'ul')
return false;
}

if (!in_array($this->_previousCall, array('_tag_close_P', '_tag_close_UL'))) {
if (!in_array($this->_previousCall, array('_tag_close_P', '_tag_close_UL'), true)) {
if ($this->_maxH) {
$this->_tag_open_BR(array());
}
Expand Down Expand Up @@ -4821,7 +4821,7 @@ protected function _tag_open_TABLE($param, $other = 'table')
if (isset($param['align'])) {
unset($param['align']);
}
if (!in_array($alignObject, array('left', 'center', 'right'))) {
if (!in_array($alignObject, array('left', 'center', 'right'), true)) {
$alignObject = 'left';
}

Expand Down Expand Up @@ -5870,7 +5870,7 @@ protected function _tag_open_INPUT($param)
$param['type'] = strtolower($param['type']);

// the type must be valid
if (!in_array($param['type'], array('text', 'checkbox', 'radio', 'hidden', 'submit', 'reset', 'button'))) {
if (!in_array($param['type'], array('text', 'checkbox', 'radio', 'hidden', 'submit', 'reset', 'button'), true)) {
$param['type'] = 'text';
}

Expand Down Expand Up @@ -6461,10 +6461,11 @@ protected function _tag_open_PATH($param)
$actions = array();
$action = array();
$lastAction = null; // last action found
for ($k=0; $k < count($path); true) {
$amountPaths = count($path);
for ($k=0; $k < $amountPaths; true) {

// for this actions, we can not have multi coordonate
if (in_array($lastAction, array('z', 'Z'))) {
if (in_array($lastAction, array('z', 'Z'), true)) {
$lastAction = null;
}

Expand Down
48 changes: 26 additions & 22 deletions src/Parsing/Css.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function resetStyle($tagName = '')

// prepare the Collapse attribute
$collapse = isset($this->value['border']['collapse']) ? $this->value['border']['collapse'] : false;
if (!in_array($tagName, array('tr', 'td', 'th', 'thead', 'tbody', 'tfoot'))) {
if (!in_array($tagName, array('tr', 'td', 'th', 'thead', 'tbody', 'tfoot'), true)) {
$collapse = false;
}

Expand Down Expand Up @@ -235,11 +235,11 @@ public function resetStyle($tagName = '')
);

// specific values for some tags
if (!in_array($tagName, array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'))) {
if (!in_array($tagName, array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'), true)) {
$this->value['margin'] = array('t'=>0,'r'=>0,'b'=>0,'l'=>0);
}

if (in_array($tagName, array('input', 'select', 'textarea'))) {
if (in_array($tagName, array('input', 'select', 'textarea'), true)) {
$this->value['border']['t'] = null;
$this->value['border']['r'] = null;
$this->value['border']['b'] = null;
Expand All @@ -257,11 +257,11 @@ public function resetStyle($tagName = '')
}
$this->value['margin-auto'] = false;

if (in_array($tagName, array('blockquote', 'div', 'fieldset'))) {
if (in_array($tagName, array('blockquote', 'div', 'fieldset'), true)) {
$this->value['vertical-align'] = 'top';
}

if (in_array($tagName, array('fieldset', 'legend'))) {
if (in_array($tagName, array('fieldset', 'legend'), true)) {
$this->value['border'] = array(
't' => $border,
'r' => $border,
Expand All @@ -277,12 +277,12 @@ public function resetStyle($tagName = '')
);
}

if (in_array($tagName, array('ul', 'li'))) {
if (in_array($tagName, array('ul', 'li'), true)) {
$this->value['list-style-type'] = '';
$this->value['list-style-image'] = '';
}

if (!in_array($tagName, array('tr', 'td'))) {
if (!in_array($tagName, array('tr', 'td'), true)) {
$this->value['padding'] = array(
't' => 0,
'r' => 0,
Expand Down Expand Up @@ -674,6 +674,8 @@ public function analyse($tagName, &$param, $legacy = null)
$correctWidth = false;
$noWidth = true;

$stylesSolidDottedDashed = array('solid', 'dotted', 'dashed');

// read all the css styles
foreach ($styles as $nom => $val) {
switch ($nom) {
Expand All @@ -695,17 +697,17 @@ public function analyse($tagName, &$param, $legacy = null)

case 'text-decoration':
$val = explode(' ', $val);
$this->value['font-underline'] = (in_array('underline', $val));
$this->value['font-overline'] = (in_array('overline', $val));
$this->value['font-linethrough'] = (in_array('line-through', $val));
$this->value['font-underline'] = in_array('underline', $val, true);
$this->value['font-overline'] = in_array('overline', $val, true);
$this->value['font-linethrough'] = in_array('line-through', $val, true);
break;

case 'text-indent':
$this->value['text-indent'] = $this->cssConverter->convertToMM($val);
break;

case 'text-transform':
if (!in_array($val, array('none', 'capitalize', 'uppercase', 'lowercase'))) {
if (!in_array($val, array('none', 'capitalize', 'uppercase', 'lowercase'), true)) {
$val = 'none';
}
$this->value['text-transform'] = $val;
Expand All @@ -731,7 +733,7 @@ public function analyse($tagName, &$param, $legacy = null)

case 'text-align':
$val = strtolower($val);
if (!in_array($val, array('left', 'right', 'center', 'justify', 'li_right'))) {
if (!in_array($val, array('left', 'right', 'center', 'justify', 'li_right'), true)) {
$val = 'left';
}
$this->value['text-align'] = $val;
Expand Down Expand Up @@ -771,7 +773,7 @@ public function analyse($tagName, &$param, $legacy = null)
break;

case 'overflow':
if (!in_array($val, array('visible', 'hidden'))) {
if (!in_array($val, array('visible', 'hidden'), true)) {
$val = 'visible';
}
$this->value['overflow'] = $val;
Expand Down Expand Up @@ -860,7 +862,7 @@ public function analyse($tagName, &$param, $legacy = null)
case 'border-style':
$val = explode(' ', $val);
foreach ($val as $valK => $valV) {
if (!in_array($valV, array('solid', 'dotted', 'dashed'))) {
if (!in_array($valV, $stylesSolidDottedDashed, true)) {
$val[$valK] = null;
}
}
Expand All @@ -880,25 +882,25 @@ public function analyse($tagName, &$param, $legacy = null)
break;

case 'border-top-style':
if (in_array($val, array('solid', 'dotted', 'dashed'))) {
if (in_array($val, $stylesSolidDottedDashed, true)) {
$this->value['border']['t']['type'] = $val;
}
break;

case 'border-right-style':
if (in_array($val, array('solid', 'dotted', 'dashed'))) {
if (in_array($val, $stylesSolidDottedDashed, true)) {
$this->value['border']['r']['type'] = $val;
}
break;

case 'border-bottom-style':
if (in_array($val, array('solid', 'dotted', 'dashed'))) {
if (in_array($val, $stylesSolidDottedDashed, true)) {
$this->value['border']['b']['type'] = $val;
}
break;

case 'border-left-style':
if (in_array($val, array('solid', 'dotted', 'dashed'))) {
if (in_array($val, $stylesSolidDottedDashed, true)) {
$this->value['border']['l']['type'] = $val;
}
break;
Expand Down Expand Up @@ -1201,18 +1203,18 @@ public function analyse($tagName, &$param, $legacy = null)

// correction on the width (quick box)
if ($noWidth
&& in_array($tagName, array('div', 'blockquote', 'fieldset'))
&& in_array($tagName, array('div', 'blockquote', 'fieldset'), true)
&& $this->value['position'] !== 'absolute'
) {
$this->value['width'] = $this->getLastWidth();
$this->value['width']-= $this->value['margin']['l'] + $this->value['margin']['r'];
} else {
if ($correctWidth) {
if (!in_array($tagName, array('table', 'div', 'blockquote', 'fieldset', 'hr'))) {
if (!in_array($tagName, array('table', 'div', 'blockquote', 'fieldset', 'hr'), true)) {
$this->value['width']-= $this->value['padding']['l'] + $this->value['padding']['r'];
$this->value['width']-= $this->value['border']['l']['width'] + $this->value['border']['r']['width'];
}
if (in_array($tagName, array('th', 'td'))) {
if (in_array($tagName, array('th', 'td'), true)) {
$this->value['width']-= $this->cssConverter->convertToMM(isset($param['cellspacing']) ? $param['cellspacing'] : '2px');
$return = false;
}
Expand Down Expand Up @@ -1509,6 +1511,8 @@ public function readBorder($css)
}
$css = array_values($css);

$stylesSolidDottedDashedDouble = array('solid', 'dotted', 'dashed', 'double');

// read the values
$res = null;
foreach ($css as $value) {
Expand All @@ -1525,7 +1529,7 @@ public function readBorder($css)
if ($tmp !== null) {
$width = $tmp;
// else, it could be the type
} elseif (in_array($value, array('solid', 'dotted', 'dashed', 'double'))) {
} elseif (in_array($value, $stylesSolidDottedDashedDouble, true)) {
$type = $value;
// else, it could be the color
} else {
Expand Down
26 changes: 15 additions & 11 deletions src/Parsing/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ public function parse($tokens)
// if it is a Text
if ($actions[$k]->getName() === 'write') {
// if the tag before the text is a tag to clean => ltrim on the text
if ($k>0 && in_array($actions[$k - 1]->getName(), $tagsToClean)) {
if ($k>0 && in_array($actions[$k - 1]->getName(), $tagsToClean, true)) {
$actions[$k]->setParam('txt', ltrim($actions[$k]->getParam('txt')));
}

// if the tag after the text is a tag to clean => rtrim on the text
if ($k < $nb - 1 && in_array($actions[$k + 1]->getName(), $tagsToClean)) {
if ($k < $nb - 1 && in_array($actions[$k + 1]->getName(), $tagsToClean, true)) {
$actions[$k]->setParam('txt', rtrim($actions[$k]->getParam('txt')));
}

Expand Down Expand Up @@ -161,27 +161,31 @@ protected function getTagAction(Token $token, &$parents)
$node->setLine($token->getLine());

$actions = array();

$nodeName = $node->getName();
$nodeIsAutoClose = $node->isAutoClose();

// if the tag must be closed
if (!in_array($node->getName(), $tagsNotClosed)) {
if (!in_array($nodeName, $tagsNotClosed, true)) {
// if it is a closure tag
if ($node->isClose()) {
// HTML validation
if (count($parents) < 1) {
$e = new HtmlParsingException('Too many tag closures found for ['.$node->getName().']');
$e->setInvalidTag($node->getName());
$e = new HtmlParsingException('Too many tag closures found for ['.$nodeName.']');
$e->setInvalidTag($nodeName);
$e->setHtmlLine($token->getLine());
throw $e;
} elseif (end($parents) != $node->getName()) {
$e = new HtmlParsingException('Tags are closed in a wrong order for ['.$node->getName().']');
$e->setInvalidTag($node->getName());
} elseif (end($parents) !== $nodeName) {
$e = new HtmlParsingException('Tags are closed in a wrong order for ['.$nodeName.']');
$e->setInvalidTag($nodeName);
$e->setHtmlLine($token->getLine());
throw $e;
} else {
array_pop($parents);
}
} else {
// if it is an auto-closed tag
if ($node->isAutoClose()) {
if ($nodeIsAutoClose) {
// save the opened tag
$actions[] = $node;

Expand All @@ -191,12 +195,12 @@ protected function getTagAction(Token $token, &$parents)
$node->setClose(true);
} else {
// else: add a child for validation
array_push($parents, $node->getName());
array_push($parents, $nodeName);
}
}

// if it is a <pre> tag (or <code> tag) not auto-closed => update the flag
if (($node->getName() === 'pre' || $node->getName() === 'code') && !$node->isAutoClose()) {
if (($nodeName === 'pre' || $nodeName === 'code') && !$nodeIsAutoClose) {
$this->tagPreIn = !$node->isClose();
}
}
Expand Down