Skip to content

Commit

Permalink
Coding standards: Also check Cpdf class
Browse files Browse the repository at this point in the history
* Enforce `elseif` as it is used in most places already
* Use `function ()` instead of `function()` as the style used more
frequently
  • Loading branch information
Mellthas committed Jun 21, 2022
1 parent 7f5d0dc commit 36be0c2
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -35,7 +35,7 @@ jobs:
run: vendor/bin/phpunit

- name: Run code sniffer
run: vendor/bin/phpcs --standard=phpcs.xml src tests
run: vendor/bin/phpcs --standard=phpcs.xml src tests lib

# - name: Upload to Scrutinizer
# continue-on-error: true
Expand Down
27 changes: 14 additions & 13 deletions lib/Cpdf.php
Expand Up @@ -1048,7 +1048,7 @@ protected function getFontSubsettingTag(array $font): string
}
}

return 'SUB' . str_pad($base_26, 3 , 'A', STR_PAD_LEFT);
return 'SUB' . str_pad($base_26, 3, 'A', STR_PAD_LEFT);
}

/**
Expand Down Expand Up @@ -1381,7 +1381,7 @@ protected function o_toUnicode($id, $action)

$res = "\n$id 0 obj\n";
$res .= "<</Length " . mb_strlen($stream, '8bit') . " >>\n";
$res .= "stream\n" . $stream . "\nendstream" . "\nendobj";;
$res .= "stream\n" . $stream . "\nendstream" . "\nendobj";

return $res;
}
Expand Down Expand Up @@ -2423,8 +2423,7 @@ protected function o_xobject($id, $action, $options = '')
$res = "\n$id 0 obj\n<< /Type /XObject\n";

foreach ($o["info"] as $k => $v) {
switch($k)
{
switch ($k) {
case 'Subtype':
$res .= "/Subtype /$v\n";
break;
Expand Down Expand Up @@ -2477,7 +2476,7 @@ protected function o_xobject($id, $action, $options = '')
}

$res .= "/Length " . mb_strlen($tmp, '8bit') . " >>\n";
$res .= "stream\n" . $tmp . "\nendstream" . "\nendobj";;
$res .= "stream\n" . $tmp . "\nendstream" . "\nendobj";

return $res;
}
Expand Down Expand Up @@ -2515,7 +2514,7 @@ protected function o_acroform($id, $action, $options = '')
$res = "\n$id 0 obj\n<<";

foreach ($o["info"] as $k => $v) {
switch($k) {
switch ($k) {
case 'Fields':
$res .= " /Fields [";
foreach ($v as $i) {
Expand Down Expand Up @@ -2619,7 +2618,7 @@ protected function o_field($id, $action, $options = '')
$res .= ">>\n";
break;
case 'T':
if($encrypted) {
if ($encrypted) {
$v = $this->filterText($this->ARC4($v), false, false);
}
$res .= "/T ($v)\n";
Expand Down Expand Up @@ -2662,7 +2661,7 @@ protected function o_sig($id, $action, $options = '')
$pos = strpos($content, sprintf("/ByteRange [ %'.010d", $id));
$len = strlen('/ByteRange [ ********** ********** ********** ********** ]');
$rangeStartPos = $pos + $len + 1 + 10; // before '<'
$content = substr_replace($content, str_pad(sprintf('/ByteRange [ 0 %u %u %u ]', $rangeStartPos, $rangeStartPos + $sign_maxlen + 2, $content_len - 2 - $sign_maxlen - $rangeStartPos ), $len, ' ', STR_PAD_RIGHT), $pos, $len);
$content = substr_replace($content, str_pad(sprintf('/ByteRange [ 0 %u %u %u ]', $rangeStartPos, $rangeStartPos + $sign_maxlen + 2, $content_len - 2 - $sign_maxlen - $rangeStartPos), $len, ' ', STR_PAD_RIGHT), $pos, $len);

$fuid = uniqid();
$tmpInput = $this->tmp . "/pkcs7.tmp." . $fuid . '.in';
Expand Down Expand Up @@ -2726,7 +2725,7 @@ protected function o_sig($id, $action, $options = '')

$o = &$this->objects[$id];
foreach ($o['info'] as $k => $v) {
switch($k) {
switch ($k) {
case 'Name':
case 'Location':
case 'Reason':
Expand Down Expand Up @@ -2852,7 +2851,7 @@ protected function o_indirect_references($id, $action, $options = null)
case 'out':
$res = "\n$id 0 obj << ";

foreach($this->objects[$id]['info'] as $referenceObjName => $referenceObjId) {
foreach ($this->objects[$id]['info'] as $referenceObjName => $referenceObjId) {
$res .= "/$referenceObjName $referenceObjId 0 R ";
}

Expand Down Expand Up @@ -4335,8 +4334,10 @@ public function addFormField($type, $name, $x0, $y0, $x1, $y1, $ff = 0, $size =
$color = implode(' ', $color) . ' rg';

$currentFontNum = $this->currentFontNum;
$font = array_filter($this->objects[$this->currentNode]['info']['fonts'],
function($item) use ($currentFontNum) { return $item['fontNum'] == $currentFontNum; });
$font = array_filter(
$this->objects[$this->currentNode]['info']['fonts'],
function ($item) use ($currentFontNum) { return $item['fontNum'] == $currentFontNum; }
);

$this->o_acroform($this->acroFormId, 'font',
['objNum' => $font[0]['objNum'], 'fontNum' => $font[0]['fontNum']]);
Expand Down Expand Up @@ -4852,7 +4853,7 @@ function filterText($text, $bom = true, $convert_encoding = true)
//$text = html_entity_decode($text, ENT_QUOTES);
$text = mb_convert_encoding($text, self::$targetEncoding, 'UTF-8');
}
} else if ($bom) {
} elseif ($bom) {
$text = $this->utf8toUtf16BE($text, $bom);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/res/html.css
Expand Up @@ -10,7 +10,7 @@
*
* Portions from Mozilla
* @link https://dxr.mozilla.org/mozilla-central/source/layout/style/res/html.css
* @license http://mozilla.org/MPL/2.0/ Mozilla Public License, v. 2.0
* @license http://mozilla.org/MPL/2.0/ Mozilla Public License, v. 2.0
*
* Portions from W3C
* @link https://drafts.csswg.org/css-ui-3/#default-style-sheet
Expand Down
6 changes: 0 additions & 6 deletions phpcs.xml
Expand Up @@ -34,9 +34,6 @@
<rule ref="Squiz.WhiteSpace.ControlStructureSpacing.SpacingBeforeClose">
<severity>0</severity>
</rule>
<rule ref="Squiz.WhiteSpace.ControlStructureSpacing.SpacingAfterOpenBrace">
<severity>0</severity>
</rule>
<rule ref="PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace">
<severity>0</severity>
</rule>
Expand All @@ -52,9 +49,6 @@
<rule ref="Generic.ControlStructures.InlineControlStructure.NotAllowed">
<severity>0</severity>
</rule>
<rule ref="PSR2.ControlStructures.ElseIfDeclaration.NotAllowed">
<severity>0</severity>
</rule>
<rule ref="PSR2.ControlStructures.SwitchDeclaration.BodyOnNextLineCASE">
<severity>0</severity>
</rule>
Expand Down
2 changes: 1 addition & 1 deletion src/Css/Color.php
Expand Up @@ -294,7 +294,7 @@ static function parse($color)
return null;
}

$values = array_map(function($c) {
$values = array_map(function ($c) {
return min(1.0, max(0.0, floatval(trim($c))));
}, $values);

Expand Down
2 changes: 1 addition & 1 deletion src/Css/Style.php
Expand Up @@ -3580,7 +3580,7 @@ protected function _get_transform_origin($computed)
$values = array_map(function ($value) {
if (\in_array($value, ["top", "left"], true)) {
return 0;
} else if (\in_array($value, ["bottom", "right"], true)) {
} elseif (\in_array($value, ["bottom", "right"], true)) {
return "100%";
} else {
return $value;
Expand Down
2 changes: 1 addition & 1 deletion src/Css/Stylesheet.php
Expand Up @@ -1301,7 +1301,7 @@ private function _parse_css($str)
$media_query_feature = strtolower($media_query_match[3]);
$media_query_value = strtolower($media_query_match[2]);
$mq[] = [$media_query_feature, $media_query_value];
} else if (empty($media_query_match[4]) === false) {
} elseif (empty($media_query_match[4]) === false) {
$media_query_feature = strtolower($media_query_match[5]);
$media_query_value = (array_key_exists(8, $media_query_match) ? strtolower($media_query_match[8]) : null);
$mq[] = [$media_query_feature, $media_query_value];
Expand Down
4 changes: 2 additions & 2 deletions src/FontMetrics.php
Expand Up @@ -549,15 +549,15 @@ public function setFontFamilies()
if (isset($this->bundledFonts) && is_array($this->bundledFonts)) {
foreach ($this->bundledFonts as $family => $variants) {
if (!isset($fontFamilies[$family])) {
$fontFamilies[$family] = array_map(function($variant) {
$fontFamilies[$family] = array_map(function ($variant) {
return $this->getOptions()->getRootDir() . '/lib/fonts/' . $variant;
}, $variants);
}
}
}
if (isset($this->userFonts) && is_array($this->userFonts)) {
foreach ($this->userFonts as $family => $variants) {
$fontFamilies[$family] = array_map(function($variant) {
$fontFamilies[$family] = array_map(function ($variant) {
$variantName = basename($variant);
if ($variantName === $variant) {
return $this->getOptions()->getFontDir() . '/' . $variant;
Expand Down
2 changes: 1 addition & 1 deletion src/FrameDecorator/Page.php
Expand Up @@ -638,7 +638,7 @@ function check_page_break(Frame $frame)

if ($next->is_table() && !$iter->is_table()) {
$this->_in_table++;
} else if (!$next->is_table() && $iter->is_table()) {
} elseif (!$next->is_table() && $iter->is_table()) {
$this->_in_table--;
}

Expand Down
2 changes: 1 addition & 1 deletion src/FrameReflower/Table.php
Expand Up @@ -93,7 +93,7 @@ protected function _assign_widths()
} else {
if ($max_width + $delta < $cb["w"]) {
$width = $max_width;
} else if ($cb["w"] - $delta > $min_width) {
} elseif ($cb["w"] - $delta > $min_width) {
$width = $cb["w"] - $delta;
} else {
$width = $min_width;
Expand Down
4 changes: 2 additions & 2 deletions src/FrameReflower/Text.php
Expand Up @@ -530,7 +530,7 @@ public function get_min_max_width(): array
case "pre":
// Find the longest line
$lines = array_flip(preg_split("/\R/u", $visible_text));
array_walk($lines, function(&$chunked_text_width, $chunked_text) use ($fontMetrics, $font, $size, $word_spacing, $letter_spacing) {
array_walk($lines, function (&$chunked_text_width, $chunked_text) use ($fontMetrics, $font, $size, $word_spacing, $letter_spacing) {
$chunked_text_width = $fontMetrics->getTextWidth($chunked_text, $font, $size, $word_spacing, $letter_spacing);
});
arsort($lines);
Expand All @@ -553,7 +553,7 @@ public function get_min_max_width(): array
case "pre-wrap":
// Find the longest line
$lines = array_flip(preg_split("/\R/u", $visible_text));
array_walk($lines, function(&$chunked_text_width, $chunked_text) use ($fontMetrics, $font, $size, $word_spacing, $letter_spacing) {
array_walk($lines, function (&$chunked_text_width, $chunked_text) use ($fontMetrics, $font, $size, $word_spacing, $letter_spacing) {
$chunked_text_width = $fontMetrics->getTextWidth($chunked_text, $font, $size, $word_spacing, $letter_spacing);
});
arsort($lines);
Expand Down
6 changes: 3 additions & 3 deletions src/Helpers.php
Expand Up @@ -592,12 +592,12 @@ public static function unichr($c)
{
if ($c <= 0x7F) {
return chr($c);
} else if ($c <= 0x7FF) {
} elseif ($c <= 0x7FF) {
return chr(0xC0 | $c >> 6) . chr(0x80 | $c & 0x3F);
} else if ($c <= 0xFFFF) {
} elseif ($c <= 0xFFFF) {
return chr(0xE0 | $c >> 12) . chr(0x80 | $c >> 6 & 0x3F)
. chr(0x80 | $c & 0x3F);
} else if ($c <= 0x10FFFF) {
} elseif ($c <= 0x10FFFF) {
return chr(0xF0 | $c >> 18) . chr(0x80 | $c >> 12 & 0x3F)
. chr(0x80 | $c >> 6 & 0x3F)
. chr(0x80 | $c & 0x3F);
Expand Down
6 changes: 3 additions & 3 deletions src/Renderer/AbstractRenderer.php
Expand Up @@ -568,7 +568,7 @@ protected function _background_image($url, $x, $y, $width, $height, $style)
// Simply place the image on the background
imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $img_h);

} else if ($repeat === "repeat-x") {
} elseif ($repeat === "repeat-x") {
for ($bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w) {
if ($bg_x < 0) {
$dst_x = 0;
Expand All @@ -581,7 +581,7 @@ protected function _background_image($url, $x, $y, $width, $height, $style)
}
imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $img_h);
}
} else if ($repeat === "repeat-y") {
} elseif ($repeat === "repeat-y") {

for ($bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h) {
if ($bg_y < 0) {
Expand All @@ -595,7 +595,7 @@ protected function _background_image($url, $x, $y, $width, $height, $style)
}
imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $h);
}
} else if ($repeat === "repeat") {
} elseif ($repeat === "repeat") {
for ($bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h) {
for ($bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w) {
if ($bg_x < 0) {
Expand Down
2 changes: 1 addition & 1 deletion tests/DompdfTest.php
Expand Up @@ -34,7 +34,7 @@ public function testSetters()
$dompdf = new Dompdf();
$dompdf->setBaseHost('test1');
$dompdf->setBasePath('test2');
$dompdf->setCallbacks(['test' => ['event' => 'test', 'f' => function() {}]]);
$dompdf->setCallbacks(['test' => ['event' => 'test', 'f' => function () {}]]);
$dompdf->setCss(new Stylesheet($dompdf));
$dompdf->setDom(new DOMDocument());
$dompdf->setHttpContext(fopen(__DIR__ . "/_files/jamaica.jpg", 'r'));
Expand Down

0 comments on commit 36be0c2

Please sign in to comment.