Skip to content

Commit

Permalink
Set phpstam max level and fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Nov 10, 2023
1 parent 2565c40 commit f95252b
Show file tree
Hide file tree
Showing 16 changed files with 3,006 additions and 1,652 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 5
level: max
paths:
- src
- test
Expand Down
220 changes: 152 additions & 68 deletions src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
/**
* Box.php
*
* @since 2011-05-23
* @category Library
* @package PdfPage
* @author Nicola Asuni <[email protected]>
* @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD
* @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
* @link https://github.com/tecnickcom/tc-lib-pdf-page
* @since 2011-05-23
* @category Library
* @package PdfPage
* @author Nicola Asuni <[email protected]>
* @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD
* @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
* @link https://github.com/tecnickcom/tc-lib-pdf-page
*
* This file is part of tc-lib-pdf-page software library.
*/
Expand All @@ -22,54 +22,71 @@
/**
* Com\Tecnick\Pdf\Page\Box
*
* @since 2011-05-23
* @category Library
* @package PdfPage
* @author Nicola Asuni <[email protected]>
* @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD
* @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
* @link https://github.com/tecnickcom/tc-lib-pdf-page
* @since 2011-05-23
* @category Library
* @package PdfPage
* @author Nicola Asuni <[email protected]>
* @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD
* @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
* @link https://github.com/tecnickcom/tc-lib-pdf-page
*/
abstract class Box extends \Com\Tecnick\Pdf\Page\Mode
{
/**
* Unit of measure conversion ratio.
*
* @var float
*/
protected $kunit = 1.0;
protected float $kunit = 1.0;

/**
* Color object.
*
* @var Color
*/
protected $col;
protected Color $col;

/**
* Array of page box names.
* Page box names.
*
* @var array
* @var array<string>
*/
public static $box = array(
public const BOX = [
'MediaBox',
'CropBox',
'BleedBox',
'TrimBox',
'ArtBox'
);
'ArtBox',
];

/**
* Swap X and Y coordinates of page boxes (change page boxes orientation).
*
* @param array $dims Array of page dimensions.
*
* @return array Page dimensions.
* @param array<string, array{
* 'llx': float,
* 'lly': float,
* 'urx': float,
* 'ury': float,
* 'bci'?: array{
* 'color': string,
* 'width': float,
* 'style': string,
* 'dash': array<int>,
* },
* }> $dims Array of page dimensions.
*
* @return array<string, array{
* 'llx': float,
* 'lly': float,
* 'urx': float,
* 'ury': float,
* 'bci'?: array{
* 'color': string,
* 'width': float,
* 'style': string,
* 'dash': array<int>,
* },
* }> Page dimensions.
*/
public function swapCoordinates(array $dims)
public function swapCoordinates(array $dims): array
{
foreach (self::$box as $type) {
foreach (self::BOX as $type) {
// swap X and Y coordinates
if (isset($dims[$type])) {
$tmp = $dims[$type]['llx'];
Expand All @@ -80,45 +97,83 @@ public function swapCoordinates(array $dims)
$dims[$type]['ury'] = $tmp;
}
}

return $dims;
}

/**
* Set page boundaries.
*
* @param array $dims Array of page dimensions to modify.
* @param string $type Box type: MediaBox, CropBox, BleedBox, TrimBox, ArtBox.
* @param float $llx Lower-left x coordinate in user units.
* @param float $lly Lower-left y coordinate in user units.
* @param float $urx Upper-right x coordinate in user units.
* @param float $ury Upper-right y coordinate in user units.
* @param array $bci BoxColorInfo: guideline style (color, width, style, dash).
* @param array<string, array{
* 'llx': float,
* 'lly': float,
* 'urx': float,
* 'ury': float,
* 'bci'?: array{
* 'color': string,
* 'width': float,
* 'style': string,
* 'dash': array<int>,
* },
* }> $dims Array of page dimensions to modify.
* @param string $type Box type: MediaBox, CropBox, BleedBox, TrimBox, ArtBox.
* @param float $llx Lower-left x coordinate in user units.
* @param float $lly Lower-left y coordinate in user units.
* @param float $urx Upper-right x coordinate in user units.
* @param float $ury Upper-right y coordinate in user units.
* @param array{
* 'color': string,
* 'width': float,
* 'style': string,
* 'dash': array<int>,
* } $bci BoxColorInfo: guideline style (color, width, style, dash).
*
* @return array Page dimensions.
* @return array<string, array{
* 'llx': float,
* 'lly': float,
* 'urx': float,
* 'ury': float,
* 'bci': array{
* 'color': string,
* 'width': float,
* 'style': string,
* 'dash': array<int>,
* },
* }> Page dimensions.
*/
public function setBox($dims, $type, $llx, $lly, $urx, $ury, array $bci = array())
{
if (empty($dims)) {
public function setBox(
array $dims,
string $type,
float $llx,
float $lly,
float $urx,
float $ury,
?array $bci = null,
): array {
if ($dims === []) {
// initialize array
$dims = array();
$dims = [];
}
if (!in_array($type, self::$box)) {

if (! in_array($type, self::BOX)) {
throw new PageException('unknown page box type: ' . $type);
}

$dims[$type]['llx'] = $llx;
$dims[$type]['lly'] = $lly;
$dims[$type]['urx'] = $urx;
$dims[$type]['ury'] = $ury;

if (empty($bci)) {
if ($bci === null) {
// set default values
$bci = array(
$bci = [
'color' => '#000000',
'width' => (1.0 / $this->kunit),
'style' => 'S', // S = solid; D = dash
'dash' => array(3)
);
'dash' => [3],
];
}

$dims[$type]['bci'] = $bci;

return $dims;
Expand All @@ -130,33 +185,49 @@ public function setBox($dims, $type, $llx, $lly, $urx, $ury, array $bci = array(
* @param float $width Page width in points.
* @param float $height Page height in points.
*
* @return array Page boxes.
* @return array<string, array{
* 'llx': float,
* 'lly': float,
* 'urx': float,
* 'ury': float,
* 'bci'?: array{
* 'color': string,
* 'width': float,
* 'style': string,
* 'dash': array<int>,
* },
* }> Page boxes.
*/
public function setPageBoxes($width, $height)
public function setPageBoxes(float $width, float $height): array
{
$dims = array();
foreach (self::$box as $type) {
$dims = [];
foreach (self::BOX as $type) {
$dims = $this->setBox($dims, $type, 0, 0, $width, $height);
}

return $dims;
}

/**
* Returns the PDF command to output the specified page boxes.
*
* @param array $dims Array of page dimensions.
*
* @return string
* @param array<string, array{
* 'llx': float,
* 'lly': float,
* 'urx': float,
* 'ury': float,
* }> $dims Array of page dimensions.
*/
protected function getBox(array $dims)
protected function getBox(array $dims): string
{
$out = '';
foreach (self::$box as $box) {
foreach (self::BOX as $box) {
if (empty($dims[$box])) {
// @codeCoverageIgnoreStart
continue;
// @codeCoverageIgnoreEnd
}

$out .= '/' . $box . ' [' . sprintf(
'%F %F %F %F',
$dims[$box]['llx'],
Expand All @@ -165,49 +236,62 @@ protected function getBox(array $dims)
$dims[$box]['ury']
) . ']' . "\n";
}

return $out;
}

/**
* Returns the PDF command to output the specified page BoxColorInfo.
*
* @param array $dims Array of page dimensions.
* @param array<string, array{
* 'bci': array{
* 'color': string,
* 'width': float,
* 'style': string,
* 'dash': array<int>,
* },
* }> $dims Array of page dimensions.
*
* @return string
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function getBoxColorInfo(array $dims)
protected function getBoxColorInfo(array $dims): string
{
$out = '/BoxColorInfo <<' . "\n";
foreach (self::$box as $box) {
foreach (self::BOX as $box) {
if (empty($dims[$box])) {
// @codeCoverageIgnoreStart
continue;
// @codeCoverageIgnoreEnd
}

$out .= '/' . $box . ' <<' . "\n";
if (!empty($dims[$box]['bci']['color'])) {
if (! empty($dims[$box]['bci']['color'])) {
$out .= '/C [' . $this->col->getPdfRgbComponents($dims[$box]['bci']['color']) . ']' . "\n";
}
if (!empty($dims[$box]['bci']['width'])) {

if (! empty($dims[$box]['bci']['width'])) {
$out .= '/W ' . sprintf('%F', ($dims[$box]['bci']['width'] * $this->kunit)) . "\n";
}
if (!empty($dims[$box]['bci']['style'])) {

if (! empty($dims[$box]['bci']['style'])) {
$mode = strtoupper($dims[$box]['bci']['style'][0]);
if ($mode !== 'D') {
$mode = 'S';
}

$out .= '/S /' . $mode . "\n";
}
if (!empty($dims[$box]['bci']['dash'])) {

if (! empty($dims[$box]['bci']['dash'])) {
$out .= '/D [';
foreach ($dims[$box]['bci']['dash'] as $dash) {
$out .= sprintf(' %F', ((float) $dash * $this->kunit));
}

$out .= ' ]' . "\n";
}

$out .= '>>' . "\n";
}
$out .= '>>' . "\n";
return $out;

return $out . ('>>' . "\n");
}
}
28 changes: 14 additions & 14 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
/**
* Exception.php
*
* @since 2011-05-23
* @category Library
* @package PdfPage
* @author Nicola Asuni <[email protected]>
* @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD
* @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
* @link https://github.com/tecnickcom/tc-lib-pdf-page
* @since 2011-05-23
* @category Library
* @package PdfPage
* @author Nicola Asuni <[email protected]>
* @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD
* @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
* @link https://github.com/tecnickcom/tc-lib-pdf-page
*
* This file is part of tc-lib-pdf-page software library.
*/
Expand All @@ -21,13 +21,13 @@
*
* Custom Exception class.
*
* @since 2011-05-23
* @category Library
* @package PdfPage
* @author Nicola Asuni <[email protected]>
* @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD
* @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
* @link https://github.com/tecnickcom/tc-lib-pdf-page
* @since 2011-05-23
* @category Library
* @package PdfPage
* @author Nicola Asuni <[email protected]>
* @copyright 2011-2023 Nicola Asuni - Tecnick.com LTD
* @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
* @link https://github.com/tecnickcom/tc-lib-pdf-page
*/
class Exception extends \Exception
{
Expand Down
Loading

0 comments on commit f95252b

Please sign in to comment.