From 163aaabc1449305f0c217b6d1b87d05baf6c9e2f Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Noblot Date: Tue, 23 Oct 2018 15:32:53 +0200 Subject: [PATCH 01/19] Add Some PHPDocs --- src/Common/Drawing.php | 2 +- src/Common/Text.php | 2 ++ src/Common/XMLWriter.php | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Common/Drawing.php b/src/Common/Drawing.php index 21417e2..d36c544 100644 --- a/src/Common/Drawing.php +++ b/src/Common/Drawing.php @@ -212,7 +212,7 @@ public static function twipsToPixels($pValue = 0) * Convert HTML hexadecimal to RGB * * @param string $pValue HTML Color in hexadecimal - * @return array Value in RGB + * @return array|false Value in RGB */ public static function htmlToRGB($pValue) { diff --git a/src/Common/Text.php b/src/Common/Text.php index 91c790a..969b6c1 100644 --- a/src/Common/Text.php +++ b/src/Common/Text.php @@ -69,6 +69,7 @@ public static function controlCharacterPHP2OOXML($value = '') * Return a number formatted for being integrated in xml files * @param float $number * @param integer $decimals + * @return string */ public static function numberFormat($number, $decimals) { @@ -79,6 +80,7 @@ public static function numberFormat($number, $decimals) * @param int $dec * @link http://stackoverflow.com/a/7153133/2235790 * @author velcrow + * @return string */ public static function chr($dec) { diff --git a/src/Common/XMLWriter.php b/src/Common/XMLWriter.php index 43e5b66..4440d65 100644 --- a/src/Common/XMLWriter.php +++ b/src/Common/XMLWriter.php @@ -50,6 +50,7 @@ class XMLWriter extends \XMLWriter * * @param int $pTemporaryStorage Temporary storage location * @param string $pTemporaryStorageDir Temporary storage folder + * @param bool $compatibility */ public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = null, $compatibility = false) { From d4803d49fb99ddc17a1f760dcc821468cf0cda65 Mon Sep 17 00:00:00 2001 From: Christopher Arzur Date: Mon, 29 Oct 2018 09:39:55 +0000 Subject: [PATCH 02/19] Added PHP 7.3 support for travis --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index e5f01f1..9521f9c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,12 +7,14 @@ php: - 7.0 - 7.1 - 7.2 + - 7.3 - hhvm matrix: allow_failures: - php: 7.1 - php: 7.2 + - php: 7.3 - php: hhvm env: From 8d457468d206a8d625e98edd7f8e886dd7240624 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sun, 24 Feb 2019 14:07:36 +1100 Subject: [PATCH 03/19] Ensure that library re-sets original xml entity loading value after doing what is needed --- src/Common/XMLReader.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Common/XMLReader.php b/src/Common/XMLReader.php index f804fb8..83e5a58 100644 --- a/src/Common/XMLReader.php +++ b/src/Common/XMLReader.php @@ -71,10 +71,11 @@ public function getDomFromZip($zipFile, $xmlFile) */ public function getDomFromString($content) { + $originalLibXMLEntityValue = libxml_disable_entity_loader(); libxml_disable_entity_loader(true); $this->dom = new \DOMDocument(); $this->dom->loadXML($content); - + libxml_disable_entity_loader($originalLibXMLEntityValue); return $this->dom; } From f6014211988415e17c649f1e0458b1d4c1cd4643 Mon Sep 17 00:00:00 2001 From: troosan Date: Wed, 6 Mar 2019 21:27:19 +0100 Subject: [PATCH 04/19] Simplify calls to libxml_disable_entity_loader --- src/Common/XMLReader.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Common/XMLReader.php b/src/Common/XMLReader.php index 83e5a58..6c720ae 100644 --- a/src/Common/XMLReader.php +++ b/src/Common/XMLReader.php @@ -71,11 +71,11 @@ public function getDomFromZip($zipFile, $xmlFile) */ public function getDomFromString($content) { - $originalLibXMLEntityValue = libxml_disable_entity_loader(); - libxml_disable_entity_loader(true); + $originalLibXMLEntityValue = libxml_disable_entity_loader(true); $this->dom = new \DOMDocument(); $this->dom->loadXML($content); libxml_disable_entity_loader($originalLibXMLEntityValue); + return $this->dom; } From 186eb888e0ca14c07e95a7c15d2b9bb5da1fd758 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Mon, 19 Jul 2021 14:30:36 +0200 Subject: [PATCH 05/19] Removed Scrutinize --- .scrutinizer.yml | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 .scrutinizer.yml diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index 6d188be..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,24 +0,0 @@ -filter: - excluded_paths: [ 'vendor/*', 'tests/*', 'samples/*' ] - -before_commands: - - "composer install --prefer-source --dev" - -tools: - php_code_sniffer: - enabled: true - config: - standard: PSR2 - php_mess_detector: - enabled: true - config: - ruleset: phpmd.xml.dist - external_code_coverage: - enabled: true - timeout: 900 - php_cpd: true - # php_sim: # Temporarily disabled to allow focus on things other than duplicates - # min_mass: 40 - php_pdepend: true - php_analyzer: true - sensiolabs_security_checker: true \ No newline at end of file From b3d2cceeacce1e62d2c62c7fe810884bc0ead75e Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Mon, 19 Jul 2021 14:30:46 +0200 Subject: [PATCH 06/19] Removed Travis CI --- .travis.yml | 62 ---------------------------------- .travis_shell_after_success.sh | 39 --------------------- 2 files changed, 101 deletions(-) delete mode 100644 .travis.yml delete mode 100644 .travis_shell_after_success.sh diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 9521f9c..0000000 --- a/.travis.yml +++ /dev/null @@ -1,62 +0,0 @@ -language: php - -php: - - 5.4 - - 5.5 - - 5.6 - - 7.0 - - 7.1 - - 7.2 - - 7.3 - - hhvm - -matrix: - allow_failures: - - php: 7.1 - - php: 7.2 - - php: 7.3 - - php: hhvm - -env: - global: - ## - secure: "LtlUOzC8FtqgbqUTmU7EU281NSCb58UFdvnz8lelNIDltBdP4eayN/TsgNIePB4jXg2d2R56ZA6j/grhE/md6jdUkulV355H3GrH/hIZmmQ+F9+87agnwLzb9+MJbqXoiE7VvjY3zGIO09G897SUfsfu6JWEcscYFlsH6KcXM6M=" - -before_script: - ## Packages - - sudo apt-get -qq update > /dev/null - - sudo apt-get -qq install graphviz > /dev/null - ## Composer - - composer self-update - - composer install --prefer-source --dev - ## PHPDocumentor - - mkdir -p build/docs - - mkdir -p build/coverage - -script: - ## PHP_CodeSniffer - - ./vendor/bin/phpcs src/ tests/ --standard=PSR2 -n - ## PHP Copy/Paste Detector - # - ./vendor/bin/phpcpd src/ - ## PHP Mess Detector - - ./vendor/bin/phpmd src/,tests/ text ./phpmd.xml.dist - ## PHPUnit - - ./vendor/bin/phpunit -c ./ --coverage-text --coverage-html ./build/coverage - ## PHPLOC - - ./vendor/bin/phploc src/ - ## PHPDocumentor - - ./vendor/bin/phpdoc -q -d ./src -t ./build/docs --template="responsive-twig" - -after_script: - ## PHPDocumentor - ## - bash .travis_shell_after_success.sh - ## Scrutinizer - - wget https://scrutinizer-ci.com/ocular.phar - - php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml - -notifications: - webhooks: - urls: - - https://webhooks.gitter.im/e/e019b92a7793d27e080c - on_success: change # options: [always|never|change] default: always - on_failure: always # options: [always|never|change] default: always - on_start: false # default: false \ No newline at end of file diff --git a/.travis_shell_after_success.sh b/.travis_shell_after_success.sh deleted file mode 100644 index bbfb7cb..0000000 --- a/.travis_shell_after_success.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash - -echo "--DEBUG--" -echo "TRAVIS_REPO_SLUG: $TRAVIS_REPO_SLUG" -echo "TRAVIS_PHP_VERSION: $TRAVIS_PHP_VERSION" -echo "TRAVIS_PULL_REQUEST: $TRAVIS_PULL_REQUEST" - -if [ "$TRAVIS_REPO_SLUG" == "PHPOffice/Common" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_PHP_VERSION" == "5.5" ]; then - - echo -e "Publishing PHPDoc...\n" - - cp -R build/docs $HOME/docs-latest - cp -R build/coverage $HOME/coverage-latest - - cd $HOME - git config --global user.email "travis@travis-ci.org" - git config --global user.name "travis-ci" - git clone --quiet --branch=gh-pages https://${GH_TOKEN}@github.com/PHPOffice/Common gh-pages > /dev/null - - cd gh-pages - echo "--DEBUG : Suppression" - git rm -rf ./docs/$TRAVIS_BRANCH - - echo "--DEBUG : Dossier" - mkdir -p docs/$TRAVIS_BRANCH - mkdir -p coverage/$TRAVIS_BRANCH - - echo "--DEBUG : Copie" - cp -Rf $HOME/docs-latest/* ./docs/$TRAVIS_BRANCH/ - cp -Rf $HOME/coverage-latest/* ./coverage/$TRAVIS_BRANCH/ - - echo "--DEBUG : Git" - git add -f . - git commit -m "PHPDocumentor (Travis Build: $TRAVIS_BUILD_NUMBER - Branch: $TRAVIS_BRANCH)" - git push -fq origin gh-pages > /dev/null - - echo -e "Published PHPDoc to gh-pages.\n" - -fi From 802af2590bc2881ec8ce908da4ec4e8418860bc7 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Mon, 19 Jul 2021 14:31:07 +0200 Subject: [PATCH 07/19] Added Github Action (Dependabot) --- .github/dependabot.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..3245e4a --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,10 @@ +version: 2 +updates: +- package-ecosystem: composer + directory: "/" + schedule: + interval: monthly + time: "11:00" + open-pull-requests-limit: 10 + assignees: + - Progi1984 From b7dc3da0a920b1fcb5b3daa33c3c4eb97408b597 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Mon, 19 Jul 2021 14:33:18 +0200 Subject: [PATCH 08/19] Added PHPCSFixer Check --- .gitignore | 2 + .php-cs-fixer.dist.php | 44 +++++++ src/Common/Adapter/Zip/PclZipAdapter.php | 11 +- src/Common/Adapter/Zip/ZipArchiveAdapter.php | 3 +- src/Common/Adapter/Zip/ZipInterface.php | 10 +- src/Common/Autoloader.php | 7 +- src/Common/Drawing.php | 70 +++++++---- src/Common/File.php | 19 ++- src/Common/Font.php | 32 +++-- src/Common/Microsoft/OLERead.php | 112 +++++++++-------- src/Common/Microsoft/PasswordEncoder.php | 113 ++++++++++-------- src/Common/Text.php | 60 ++++++---- src/Common/XMLReader.php | 21 +++- src/Common/XMLWriter.php | 18 ++- .../Adapter/Zip/AbstractZipAdapterTest.php | 5 +- .../Tests/Adapter/Zip/PclZipAdapterTest.php | 1 - .../Adapter/Zip/ZipArchiveAdapterTest.php | 1 - tests/Common/Tests/AutoloaderTest.php | 5 +- tests/Common/Tests/DrawingTest.php | 33 ++--- tests/Common/Tests/FileTest.php | 43 +++---- tests/Common/Tests/FontTest.php | 7 +- .../Tests/Microsoft/PasswordEncoderTest.php | 4 +- tests/Common/Tests/TextTest.php | 10 +- tests/Common/Tests/XMLReaderTest.php | 19 +-- tests/Common/Tests/XMLWriterTest.php | 17 ++- .../Common/Tests/_includes/TestHelperZip.php | 7 +- tests/Common/Tests/_includes/XmlDocument.php | 36 ++++-- tests/bootstrap.php | 18 +-- 28 files changed, 439 insertions(+), 289 deletions(-) create mode 100644 .php-cs-fixer.dist.php diff --git a/.gitignore b/.gitignore index 605fc80..213b97b 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ vendor /samples/Github_*.* /samples/#83/*.lnk /composer.lock + +/.php-cs-fixer.cache \ No newline at end of file diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..532c34a --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,44 @@ +setUsingCache(true) + ->setRiskyAllowed(true) + ->setRules([ + '@Symfony' => true, + 'array_indentation' => true, + 'cast_spaces' => [ + 'space' => 'single', + ], + 'combine_consecutive_issets' => true, + 'concat_space' => [ + 'spacing' => 'one', + ], + 'error_suppression' => [ + 'mute_deprecation_error' => false, + 'noise_remaining_usages' => false, + 'noise_remaining_usages_exclude' => [], + ], + 'function_to_constant' => false, + 'method_chaining_indentation' => true, + 'no_alias_functions' => false, + 'no_superfluous_phpdoc_tags' => false, + 'non_printable_character' => [ + 'use_escape_sequences_in_strings' => true, + ], + 'phpdoc_align' => [ + 'align' => 'left', + ], + 'phpdoc_summary' => false, + 'protected_to_private' => false, + 'self_accessor' => false, + 'yoda_style' => false, + 'single_line_throw' => false, + 'no_alias_language_construct_call' => false, + ]) + ->getFinder() + ->in(__DIR__) + ->exclude('vendor'); + +return $config; \ No newline at end of file diff --git a/src/Common/Adapter/Zip/PclZipAdapter.php b/src/Common/Adapter/Zip/PclZipAdapter.php index 053531f..9d31f1a 100644 --- a/src/Common/Adapter/Zip/PclZipAdapter.php +++ b/src/Common/Adapter/Zip/PclZipAdapter.php @@ -1,4 +1,5 @@ oPclZip = new PclZip($filename); $this->tmpDir = sys_get_temp_dir(); + return $this; } @@ -31,15 +33,16 @@ public function addFromString($localname, $contents) { $pathData = pathinfo($localname); - $hFile = fopen($this->tmpDir.'/'.$pathData['basename'], "wb"); + $hFile = fopen($this->tmpDir . '/' . $pathData['basename'], 'wb'); fwrite($hFile, $contents); fclose($hFile); - $res = $this->oPclZip->add($this->tmpDir.'/'.$pathData['basename'], PCLZIP_OPT_REMOVE_PATH, $this->tmpDir, PCLZIP_OPT_ADD_PATH, $pathData['dirname']); + $res = $this->oPclZip->add($this->tmpDir . '/' . $pathData['basename'], PCLZIP_OPT_REMOVE_PATH, $this->tmpDir, PCLZIP_OPT_ADD_PATH, $pathData['dirname']); if ($res == 0) { - throw new \Exception("Error zipping files : " . $this->oPclZip->errorInfo(true)); + throw new \Exception('Error zipping files : ' . $this->oPclZip->errorInfo(true)); } - unlink($this->tmpDir.'/'.$pathData['basename']); + unlink($this->tmpDir . '/' . $pathData['basename']); + return $this; } } diff --git a/src/Common/Adapter/Zip/ZipArchiveAdapter.php b/src/Common/Adapter/Zip/ZipArchiveAdapter.php index da2c346..a8728eb 100644 --- a/src/Common/Adapter/Zip/ZipArchiveAdapter.php +++ b/src/Common/Adapter/Zip/ZipArchiveAdapter.php @@ -35,13 +35,14 @@ public function close() if ($this->oZipArchive->close() === false) { throw new \Exception("Could not close zip file $this->filename."); } + return $this; } public function addFromString($localname, $contents) { if ($this->oZipArchive->addFromString($localname, $contents) === false) { - throw new \Exception("Error zipping files : " . $localname); + throw new \Exception('Error zipping files : ' . $localname); } return $this; diff --git a/src/Common/Adapter/Zip/ZipInterface.php b/src/Common/Adapter/Zip/ZipInterface.php index e8dbc8c..1e22790 100644 --- a/src/Common/Adapter/Zip/ZipInterface.php +++ b/src/Common/Adapter/Zip/ZipInterface.php @@ -6,24 +6,32 @@ interface ZipInterface { /** * Open a ZIP file archive + * * @param string $filename + * * @return $this + * * @throws \Exception */ public function open($filename); /** * Close the active archive (opened or newly created) + * * @return $this + * * @throws \Exception */ public function close(); /** * Add a file to a ZIP archive using its contents - * @param string $localname The name of the entry to create. + * + * @param string $localname the name of the entry to create * @param string $contents The contents to use to create the entry. It is used in a binary safe mode. + * * @return $this + * * @throws \Exception */ public function addFromString($localname, $contents); diff --git a/src/Common/Autoloader.php b/src/Common/Autoloader.php index 8b976c9..9dff18e 100644 --- a/src/Common/Autoloader.php +++ b/src/Common/Autoloader.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -22,7 +23,7 @@ class Autoloader { /** @const string */ - const NAMESPACE_PREFIX = 'PhpOffice\\Common\\'; + public const NAMESPACE_PREFIX = 'PhpOffice\\Common\\'; /** * Register @@ -31,7 +32,7 @@ class Autoloader */ public static function register() { - spl_autoload_register(array(new self, 'autoload')); + spl_autoload_register([new self(), 'autoload']); } /** diff --git a/src/Common/Drawing.php b/src/Common/Drawing.php index d36c544..8c7066a 100644 --- a/src/Common/Drawing.php +++ b/src/Common/Drawing.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -21,12 +22,13 @@ */ class Drawing { - const DPI_96 = 96; + public const DPI_96 = 96; /** * Convert pixels to EMU * - * @param int $pValue Value in pixels + * @param int $pValue Value in pixels + * * @return int */ public static function pixelsToEmu($pValue = 0) @@ -37,7 +39,8 @@ public static function pixelsToEmu($pValue = 0) /** * Convert EMU to pixels * - * @param int $pValue Value in EMU + * @param int $pValue Value in EMU + * * @return int */ public static function emuToPixels($pValue = 0) @@ -45,13 +48,15 @@ public static function emuToPixels($pValue = 0) if ($pValue == 0) { return 0; } + return round($pValue / 9525); } /** * Convert pixels to points * - * @param int $pValue Value in pixels + * @param int $pValue Value in pixels + * * @return float */ public static function pixelsToPoints($pValue = 0) @@ -62,7 +67,8 @@ public static function pixelsToPoints($pValue = 0) /** * Convert points width to centimeters * - * @param int $pValue Value in points + * @param int $pValue Value in points + * * @return float */ public static function pointsToCentimeters($pValue = 0) @@ -70,13 +76,15 @@ public static function pointsToCentimeters($pValue = 0) if ($pValue == 0) { return 0; } - return ((($pValue * 1.333333333) / self::DPI_96) * 2.54); + + return (($pValue * 1.333333333) / self::DPI_96) * 2.54; } - + /** * Convert points width to pixels * - * @param int $pValue Value in points + * @param int $pValue Value in points + * * @return float */ public static function pointsToPixels($pValue = 0) @@ -84,25 +92,28 @@ public static function pointsToPixels($pValue = 0) if ($pValue == 0) { return 0; } + return $pValue * 1.333333333; } /** * Convert pixels to centimeters * - * @param int $pValue Value in pixels + * @param int $pValue Value in pixels + * * @return float */ public static function pixelsToCentimeters($pValue = 0) { //return $pValue * 0.028; - return (($pValue / self::DPI_96) * 2.54); + return ($pValue / self::DPI_96) * 2.54; } /** * Convert centimeters width to pixels * - * @param int $pValue Value in centimeters + * @param int $pValue Value in centimeters + * * @return float */ public static function centimetersToPixels($pValue = 0) @@ -110,13 +121,15 @@ public static function centimetersToPixels($pValue = 0) if ($pValue == 0) { return 0; } + return ($pValue / 2.54) * self::DPI_96; } /** * Convert degrees to angle * - * @param int $pValue Degrees + * @param int $pValue Degrees + * * @return int */ public static function degreesToAngle($pValue = 0) @@ -127,7 +140,8 @@ public static function degreesToAngle($pValue = 0) /** * Convert angle to degrees * - * @param int $pValue Angle + * @param int $pValue Angle + * * @return int */ public static function angleToDegrees($pValue = 0) @@ -135,13 +149,15 @@ public static function angleToDegrees($pValue = 0) if ($pValue == 0) { return 0; } + return round($pValue / 60000); } /** * Convert centimeters width to twips * - * @param integer $pValue + * @param int $pValue + * * @return float */ public static function centimetersToTwips($pValue = 0) @@ -149,13 +165,15 @@ public static function centimetersToTwips($pValue = 0) if ($pValue == 0) { return 0; } + return $pValue * 566.928; } /** * Convert twips width to centimeters * - * @param integer $pValue + * @param int $pValue + * * @return float */ public static function twipsToCentimeters($pValue = 0) @@ -163,13 +181,15 @@ public static function twipsToCentimeters($pValue = 0) if ($pValue == 0) { return 0; } + return $pValue / 566.928; } /** * Convert inches width to twips * - * @param integer $pValue + * @param int $pValue + * * @return float */ public static function inchesToTwips($pValue = 0) @@ -177,13 +197,15 @@ public static function inchesToTwips($pValue = 0) if ($pValue == 0) { return 0; } + return $pValue * 1440; } /** * Convert twips width to inches * - * @param integer $pValue + * @param int $pValue + * * @return float */ public static function twipsToInches($pValue = 0) @@ -191,13 +213,15 @@ public static function twipsToInches($pValue = 0) if ($pValue == 0) { return 0; } + return $pValue / 1440; } /** * Convert twips width to pixels * - * @param integer $pValue + * @param int $pValue + * * @return float */ public static function twipsToPixels($pValue = 0) @@ -205,6 +229,7 @@ public static function twipsToPixels($pValue = 0) if ($pValue == 0) { return 0; } + return round($pValue / 15.873984); } @@ -212,6 +237,7 @@ public static function twipsToPixels($pValue = 0) * Convert HTML hexadecimal to RGB * * @param string $pValue HTML Color in hexadecimal + * * @return array|false Value in RGB */ public static function htmlToRGB($pValue) @@ -221,9 +247,9 @@ public static function htmlToRGB($pValue) } if (strlen($pValue) == 6) { - list($colorR, $colorG, $colorB) = array($pValue[0] . $pValue[1], $pValue[2] . $pValue[3], $pValue[4] . $pValue[5]); + list($colorR, $colorG, $colorB) = [$pValue[0] . $pValue[1], $pValue[2] . $pValue[3], $pValue[4] . $pValue[5]]; } elseif (strlen($pValue) == 3) { - list($colorR, $colorG, $colorB) = array($pValue[0] . $pValue[0], $pValue[1] . $pValue[1], $pValue[2] . $pValue[2]); + list($colorR, $colorG, $colorB) = [$pValue[0] . $pValue[0], $pValue[1] . $pValue[1], $pValue[2] . $pValue[2]]; } else { return false; } @@ -232,6 +258,6 @@ public static function htmlToRGB($pValue) $colorG = hexdec($colorG); $colorB = hexdec($colorB); - return array($colorR, $colorG, $colorB); + return [$colorR, $colorG, $colorB]; } } diff --git a/src/Common/File.php b/src/Common/File.php index 8cb5907..c04c395 100644 --- a/src/Common/File.php +++ b/src/Common/File.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -24,7 +25,8 @@ class File /** * Verify if a file exists * - * @param string $pFilename Filename + * @param string $pFilename Filename + * * @return bool */ public static function fileExists($pFilename) @@ -34,7 +36,7 @@ public static function fileExists($pFilename) // doing the original file_exists on ZIP archives... if (strtolower(substr($pFilename, 0, 3)) == 'zip') { // Open ZIP file and verify if the file exists - $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6); + $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6); $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1); $zip = new \ZipArchive(); @@ -51,10 +53,12 @@ public static function fileExists($pFilename) // Regular file_exists return file_exists($pFilename); } + /** * Returns the content of a file * - * @param string $pFilename Filename + * @param string $pFilename Filename + * * @return string */ public static function fileGetContents($pFilename) @@ -64,15 +68,17 @@ public static function fileGetContents($pFilename) } if (strtolower(substr($pFilename, 0, 3)) == 'zip') { // Open ZIP file and verify if the file exists - $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6); + $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6); $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1); $zip = new \ZipArchive(); if ($zip->open($zipFile) === true) { $returnValue = $zip->getFromName($archiveFile); $zip->close(); + return $returnValue; } + return false; } // Regular file contents @@ -82,7 +88,8 @@ public static function fileGetContents($pFilename) /** * Returns canonicalized absolute pathname, also for ZIP archives * - * @param string $pFilename + * @param string $pFilename + * * @return string */ public static function realpath($pFilename) diff --git a/src/Common/Font.php b/src/Common/Font.php index 03a8e73..eecf611 100644 --- a/src/Common/Font.php +++ b/src/Common/Font.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -24,41 +25,45 @@ class Font /** * Calculate an (approximate) pixel size, based on a font points size * - * @param int $fontSizeInPoints Font size (in points) + * @param int $fontSizeInPoints Font size (in points) + * * @return int Font size (in pixels) */ public static function fontSizeToPixels($fontSizeInPoints = 12) { - return ((16 / 12) * $fontSizeInPoints); + return (16 / 12) * $fontSizeInPoints; } /** * Calculate an (approximate) pixel size, based on inch size * - * @param int $sizeInInch Font size (in inch) + * @param int $sizeInInch Font size (in inch) + * * @return int Size (in pixels) */ public static function inchSizeToPixels($sizeInInch = 1) { - return ($sizeInInch * 96); + return $sizeInInch * 96; } /** * Calculate an (approximate) pixel size, based on centimeter size * - * @param int $sizeInCm Font size (in centimeters) + * @param int $sizeInCm Font size (in centimeters) + * * @return int Size (in pixels) */ public static function centimeterSizeToPixels($sizeInCm = 1) { - return ($sizeInCm * 37.795275591); + return $sizeInCm * 37.795275591; } /** * Convert centimeter to twip * * @param int $sizeInCm - * @return double + * + * @return float */ public static function centimeterSizeToTwips($sizeInCm = 1) { @@ -69,7 +74,8 @@ public static function centimeterSizeToTwips($sizeInCm = 1) * Convert inch to twip * * @param int $sizeInInch - * @return double + * + * @return float */ public static function inchSizeToTwips($sizeInInch = 1) { @@ -80,7 +86,8 @@ public static function inchSizeToTwips($sizeInInch = 1) * Convert pixel to twip * * @param int $sizeInPixel - * @return double + * + * @return float */ public static function pixelSizeToTwips($sizeInPixel = 1) { @@ -90,8 +97,9 @@ public static function pixelSizeToTwips($sizeInPixel = 1) /** * Calculate twip based on point size, used mainly for paragraph spacing * - * @param integer $sizeInPoint Size in point - * @return integer Size (in twips) + * @param int $sizeInPoint Size in point + * + * @return int Size (in twips) */ public static function pointSizeToTwips($sizeInPoint = 1) { diff --git a/src/Common/Microsoft/OLERead.php b/src/Common/Microsoft/OLERead.php index fa1df12..b6bb69d 100644 --- a/src/Common/Microsoft/OLERead.php +++ b/src/Common/Microsoft/OLERead.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -25,56 +26,57 @@ class OLERead private $data = ''; // OLE identifier - const IDENTIFIER_OLE = IDENTIFIER_OLE; + public const IDENTIFIER_OLE = IDENTIFIER_OLE; // Size of a sector = 512 bytes - const BIG_BLOCK_SIZE = 0x200; + public const BIG_BLOCK_SIZE = 0x200; // Size of a short sector = 64 bytes - const SMALL_BLOCK_SIZE = 0x40; + public const SMALL_BLOCK_SIZE = 0x40; // Size of a directory entry always = 128 bytes - const PROPERTY_STORAGE_BLOCK_SIZE = 0x80; + public const PROPERTY_STORAGE_BLOCK_SIZE = 0x80; // Minimum size of a standard stream = 4096 bytes, streams smaller than this are stored as short streams - const SMALL_BLOCK_THRESHOLD = 0x1000; + public const SMALL_BLOCK_THRESHOLD = 0x1000; // header offsets - const NUM_BIG_BLOCK_DEPOT_BLOCKS_POS = 0x2c; - const ROOT_START_BLOCK_POS = 0x30; - const SMALL_BLOCK_DEPOT_BLOCK_POS = 0x3c; - const EXTENSION_BLOCK_POS = 0x44; - const NUM_EXTENSION_BLOCK_POS = 0x48; - const BIG_BLOCK_DEPOT_BLOCKS_POS = 0x4c; + public const NUM_BIG_BLOCK_DEPOT_BLOCKS_POS = 0x2c; + public const ROOT_START_BLOCK_POS = 0x30; + public const SMALL_BLOCK_DEPOT_BLOCK_POS = 0x3c; + public const EXTENSION_BLOCK_POS = 0x44; + public const NUM_EXTENSION_BLOCK_POS = 0x48; + public const BIG_BLOCK_DEPOT_BLOCKS_POS = 0x4c; // property storage offsets (directory offsets) - const SIZE_OF_NAME_POS = 0x40; - const TYPE_POS = 0x42; - const START_BLOCK_POS = 0x74; - const SIZE_POS = 0x78; - - public $summaryInformation = null; - public $docSummaryInfos = null; - public $powerpointDocument = null; - public $currentUser = null; - public $pictures = null; - public $rootEntry = null; - public $props = array(); - public $smallBlockChain = null; - public $bigBlockChain = null; - public $entry = null; + public const SIZE_OF_NAME_POS = 0x40; + public const TYPE_POS = 0x42; + public const START_BLOCK_POS = 0x74; + public const SIZE_POS = 0x78; + + public $summaryInformation = null; + public $docSummaryInfos = null; + public $powerpointDocument = null; + public $currentUser = null; + public $pictures = null; + public $rootEntry = null; + public $props = []; + public $smallBlockChain = null; + public $bigBlockChain = null; + public $entry = null; /** * Read the file * * @param $sFileName string Filename + * * @throws \Exception */ public function read($sFileName) { // Check if file exists and is readable if (!is_readable($sFileName)) { - throw new \Exception("Could not open " . $sFileName . " for reading! File does not exist, or it is not readable."); + throw new \Exception('Could not open ' . $sFileName . ' for reading! File does not exist, or it is not readable.'); } // Get the file identifier @@ -104,18 +106,18 @@ public function read($sFileName) // Total number of sectors used by MSAT $numExtensionBlocks = self::getInt4d($this->data, self::NUM_EXTENSION_BLOCK_POS); - $bigBlockDepotBlocks = array(); + $bigBlockDepotBlocks = []; $pos = self::BIG_BLOCK_DEPOT_BLOCKS_POS; $bbdBlocks = $numBigBlkDepotBlks; if ($numExtensionBlocks != 0) { - $bbdBlocks = (self::BIG_BLOCK_SIZE - self::BIG_BLOCK_DEPOT_BLOCKS_POS)/4; + $bbdBlocks = (self::BIG_BLOCK_SIZE - self::BIG_BLOCK_DEPOT_BLOCKS_POS) / 4; } for ($i = 0; $i < $bbdBlocks; ++$i) { - $bigBlockDepotBlocks[$i] = self::getInt4d($this->data, $pos); - $pos += 4; + $bigBlockDepotBlocks[$i] = self::getInt4d($this->data, $pos); + $pos += 4; } for ($j = 0; $j < $numExtensionBlocks; ++$j) { @@ -138,8 +140,8 @@ public function read($sFileName) for ($i = 0; $i < $numBigBlkDepotBlks; ++$i) { $pos = ($bigBlockDepotBlocks[$i] + 1) * self::BIG_BLOCK_SIZE; - $this->bigBlockChain .= substr($this->data, $pos, 4*$bbs); - $pos += 4*$bbs; + $this->bigBlockChain .= substr($this->data, $pos, 4 * $bbs); + $pos += 4 * $bbs; } $sbdBlock = $sbdStartBlock; @@ -147,10 +149,10 @@ public function read($sFileName) while ($sbdBlock != -2) { $pos = ($sbdBlock + 1) * self::BIG_BLOCK_SIZE; - $this->smallBlockChain .= substr($this->data, $pos, 4*$bbs); - $pos += 4*$bbs; + $this->smallBlockChain .= substr($this->data, $pos, 4 * $bbs); + $pos += 4 * $bbs; - $sbdBlock = self::getInt4d($this->bigBlockChain, $sbdBlock*4); + $sbdBlock = self::getInt4d($this->bigBlockChain, $sbdBlock * 4); } // read the directory stream @@ -179,10 +181,10 @@ public function getStream($stream) $block = $this->props[$stream]['startBlock']; while ($block != -2) { - $pos = $block * self::SMALL_BLOCK_SIZE; + $pos = $block * self::SMALL_BLOCK_SIZE; $streamData .= substr($rootdata, $pos, self::SMALL_BLOCK_SIZE); - $block = self::getInt4d($this->smallBlockChain, $block*4); + $block = self::getInt4d($this->smallBlockChain, $block * 4); } return $streamData; @@ -202,7 +204,7 @@ public function getStream($stream) while ($block != -2) { $pos = ($block + 1) * self::BIG_BLOCK_SIZE; $streamData .= substr($this->data, $pos, self::BIG_BLOCK_SIZE); - $block = self::getInt4d($this->bigBlockChain, $block*4); + $block = self::getInt4d($this->bigBlockChain, $block * 4); } return $streamData; @@ -212,6 +214,7 @@ public function getStream($stream) * Read a standard stream (by joining sectors using information from SAT) * * @param int $blID Sector ID where the stream starts + * * @return string Data for standard stream */ private function readData($blID) @@ -222,8 +225,9 @@ private function readData($blID) while ($block != -2) { $pos = ($block + 1) * self::BIG_BLOCK_SIZE; $data .= substr($this->data, $pos, self::BIG_BLOCK_SIZE); - $block = self::getInt4d($this->bigBlockChain, $block*4); + $block = self::getInt4d($this->bigBlockChain, $block * 4); } + return $data; } @@ -241,7 +245,7 @@ private function readPropertySets() $data = substr($this->entry, $offset, self::PROPERTY_STORAGE_BLOCK_SIZE); // size in bytes of name - $nameSize = ord($data[self::SIZE_OF_NAME_POS]) | (ord($data[self::SIZE_OF_NAME_POS+1]) << 8); + $nameSize = ord($data[self::SIZE_OF_NAME_POS]) | (ord($data[self::SIZE_OF_NAME_POS + 1]) << 8); // type of entry $type = ord($data[self::TYPE_POS]); @@ -252,13 +256,13 @@ private function readPropertySets() $size = self::getInt4d($data, self::SIZE_POS); - $name = str_replace("\x00", "", substr($data, 0, $nameSize)); + $name = str_replace("\x00", '', substr($data, 0, $nameSize)); if ($size > 0) { - $this->props[] = array ( - 'name' => $name, - 'type' => $type, - 'startBlock' => $startBlock, - 'size' => $size); + $this->props[] = [ + 'name' => $name, + 'type' => $type, + 'startBlock' => $startBlock, + 'size' => $size, ]; // tmp helper to simplify checks $upName = strtoupper($name); @@ -268,14 +272,14 @@ private function readPropertySets() case 'R': $this->rootEntry = count($this->props) - 1; break; - case chr(1).'COMPOBJ': + case chr(1) . 'COMPOBJ': break; - case chr(1).'OLE': + case chr(1) . 'OLE': break; - case chr(5).'SUMMARYINFORMATION': + case chr(5) . 'SUMMARYINFORMATION': $this->summaryInformation = count($this->props) - 1; break; - case chr(5).'DOCUMENTSUMMARYINFORMATION': + case chr(5) . 'DOCUMENTSUMMARYINFORMATION': $this->docSummaryInfos = count($this->props) - 1; break; case 'CURRENT USER': @@ -288,7 +292,7 @@ private function readPropertySets() $this->powerpointDocument = count($this->props) - 1; break; default: - throw new \Exception('OLE Block Not defined: $upName : '.$upName. ' - $name : "'.$name.'"'); + throw new \Exception('OLE Block Not defined: $upName : ' . $upName . ' - $name : "' . $name . '"'); } } @@ -301,6 +305,7 @@ private function readPropertySets() * * @param string $data * @param int $pos + * * @return int */ private static function getInt4d($data, $pos) @@ -315,6 +320,7 @@ private static function getInt4d($data, $pos) } else { $ord24 = ($or24 & 127) << 24; } + return ord($data[$pos]) | (ord($data[$pos + 1]) << 8) | (ord($data[$pos + 2]) << 16) | $ord24; } } diff --git a/src/Common/Microsoft/PasswordEncoder.php b/src/Common/Microsoft/PasswordEncoder.php index f620f4c..c158e35 100644 --- a/src/Common/Microsoft/PasswordEncoder.php +++ b/src/Common/Microsoft/PasswordEncoder.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -21,39 +22,40 @@ */ class PasswordEncoder { - const ALGORITHM_MD2 = 'MD2'; - const ALGORITHM_MD4 = 'MD4'; - const ALGORITHM_MD5 = 'MD5'; - const ALGORITHM_SHA_1 = 'SHA-1'; - const ALGORITHM_SHA_256 = 'SHA-256'; - const ALGORITHM_SHA_384 = 'SHA-384'; - const ALGORITHM_SHA_512 = 'SHA-512'; - const ALGORITHM_RIPEMD = 'RIPEMD'; - const ALGORITHM_RIPEMD_160 = 'RIPEMD-160'; - const ALGORITHM_MAC = 'MAC'; - const ALGORITHM_HMAC = 'HMAC'; + public const ALGORITHM_MD2 = 'MD2'; + public const ALGORITHM_MD4 = 'MD4'; + public const ALGORITHM_MD5 = 'MD5'; + public const ALGORITHM_SHA_1 = 'SHA-1'; + public const ALGORITHM_SHA_256 = 'SHA-256'; + public const ALGORITHM_SHA_384 = 'SHA-384'; + public const ALGORITHM_SHA_512 = 'SHA-512'; + public const ALGORITHM_RIPEMD = 'RIPEMD'; + public const ALGORITHM_RIPEMD_160 = 'RIPEMD-160'; + public const ALGORITHM_MAC = 'MAC'; + public const ALGORITHM_HMAC = 'HMAC'; /** * Mapping between algorithm name and algorithm ID * * @var array + * * @see https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.writeprotection.cryptographicalgorithmsid(v=office.14).aspx */ - private static $algorithmMapping = array( - self::ALGORITHM_MD2 => array(1, 'md2'), - self::ALGORITHM_MD4 => array(2, 'md4'), - self::ALGORITHM_MD5 => array(3, 'md5'), - self::ALGORITHM_SHA_1 => array(4, 'sha1'), - self::ALGORITHM_MAC => array(5, ''), // 'mac' -> not possible with hash() - self::ALGORITHM_RIPEMD => array(6, 'ripemd'), - self::ALGORITHM_RIPEMD_160 => array(7, 'ripemd160'), - self::ALGORITHM_HMAC => array(9, ''), //'hmac' -> not possible with hash() - self::ALGORITHM_SHA_256 => array(12, 'sha256'), - self::ALGORITHM_SHA_384 => array(13, 'sha384'), - self::ALGORITHM_SHA_512 => array(14, 'sha512'), - ); - - private static $initialCodeArray = array( + private static $algorithmMapping = [ + self::ALGORITHM_MD2 => [1, 'md2'], + self::ALGORITHM_MD4 => [2, 'md4'], + self::ALGORITHM_MD5 => [3, 'md5'], + self::ALGORITHM_SHA_1 => [4, 'sha1'], + self::ALGORITHM_MAC => [5, ''], // 'mac' -> not possible with hash() + self::ALGORITHM_RIPEMD => [6, 'ripemd'], + self::ALGORITHM_RIPEMD_160 => [7, 'ripemd160'], + self::ALGORITHM_HMAC => [9, ''], //'hmac' -> not possible with hash() + self::ALGORITHM_SHA_256 => [12, 'sha256'], + self::ALGORITHM_SHA_384 => [13, 'sha384'], + self::ALGORITHM_SHA_512 => [14, 'sha512'], + ]; + + private static $initialCodeArray = [ 0xE1F0, 0x1D0F, 0xCC9C, @@ -69,36 +71,38 @@ class PasswordEncoder 0x280C, 0xA96A, 0x4EC3, - ); - - private static $encryptionMatrix = array( - array(0xAEFC, 0x4DD9, 0x9BB2, 0x2745, 0x4E8A, 0x9D14, 0x2A09), - array(0x7B61, 0xF6C2, 0xFDA5, 0xEB6B, 0xC6F7, 0x9DCF, 0x2BBF), - array(0x4563, 0x8AC6, 0x05AD, 0x0B5A, 0x16B4, 0x2D68, 0x5AD0), - array(0x0375, 0x06EA, 0x0DD4, 0x1BA8, 0x3750, 0x6EA0, 0xDD40), - array(0xD849, 0xA0B3, 0x5147, 0xA28E, 0x553D, 0xAA7A, 0x44D5), - array(0x6F45, 0xDE8A, 0xAD35, 0x4A4B, 0x9496, 0x390D, 0x721A), - array(0xEB23, 0xC667, 0x9CEF, 0x29FF, 0x53FE, 0xA7FC, 0x5FD9), - array(0x47D3, 0x8FA6, 0x0F6D, 0x1EDA, 0x3DB4, 0x7B68, 0xF6D0), - array(0xB861, 0x60E3, 0xC1C6, 0x93AD, 0x377B, 0x6EF6, 0xDDEC), - array(0x45A0, 0x8B40, 0x06A1, 0x0D42, 0x1A84, 0x3508, 0x6A10), - array(0xAA51, 0x4483, 0x8906, 0x022D, 0x045A, 0x08B4, 0x1168), - array(0x76B4, 0xED68, 0xCAF1, 0x85C3, 0x1BA7, 0x374E, 0x6E9C), - array(0x3730, 0x6E60, 0xDCC0, 0xA9A1, 0x4363, 0x86C6, 0x1DAD), - array(0x3331, 0x6662, 0xCCC4, 0x89A9, 0x0373, 0x06E6, 0x0DCC), - array(0x1021, 0x2042, 0x4084, 0x8108, 0x1231, 0x2462, 0x48C4), - ); + ]; + + private static $encryptionMatrix = [ + [0xAEFC, 0x4DD9, 0x9BB2, 0x2745, 0x4E8A, 0x9D14, 0x2A09], + [0x7B61, 0xF6C2, 0xFDA5, 0xEB6B, 0xC6F7, 0x9DCF, 0x2BBF], + [0x4563, 0x8AC6, 0x05AD, 0x0B5A, 0x16B4, 0x2D68, 0x5AD0], + [0x0375, 0x06EA, 0x0DD4, 0x1BA8, 0x3750, 0x6EA0, 0xDD40], + [0xD849, 0xA0B3, 0x5147, 0xA28E, 0x553D, 0xAA7A, 0x44D5], + [0x6F45, 0xDE8A, 0xAD35, 0x4A4B, 0x9496, 0x390D, 0x721A], + [0xEB23, 0xC667, 0x9CEF, 0x29FF, 0x53FE, 0xA7FC, 0x5FD9], + [0x47D3, 0x8FA6, 0x0F6D, 0x1EDA, 0x3DB4, 0x7B68, 0xF6D0], + [0xB861, 0x60E3, 0xC1C6, 0x93AD, 0x377B, 0x6EF6, 0xDDEC], + [0x45A0, 0x8B40, 0x06A1, 0x0D42, 0x1A84, 0x3508, 0x6A10], + [0xAA51, 0x4483, 0x8906, 0x022D, 0x045A, 0x08B4, 0x1168], + [0x76B4, 0xED68, 0xCAF1, 0x85C3, 0x1BA7, 0x374E, 0x6E9C], + [0x3730, 0x6E60, 0xDCC0, 0xA9A1, 0x4363, 0x86C6, 0x1DAD], + [0x3331, 0x6662, 0xCCC4, 0x89A9, 0x0373, 0x06E6, 0x0DCC], + [0x1021, 0x2042, 0x4084, 0x8108, 0x1231, 0x2462, 0x48C4], + ]; private static $passwordMaxLength = 15; /** * Create a hashed password that MS Word will be able to work with + * * @see https://blogs.msdn.microsoft.com/vsod/2010/04/05/how-to-set-the-editing-restrictions-in-word-using-open-xml-sdk-2-0/ * * @param string $password * @param string $algorithmName * @param string $salt * @param int $spinCount + * * @return string */ public static function hashPassword($password, $algorithmName = self::ALGORITHM_SHA_1, $salt = null, $spinCount = 10000) @@ -111,9 +115,9 @@ public static function hashPassword($password, $algorithmName = self::ALGORITHM_ // Get the single-byte values by iterating through the Unicode characters of the truncated password. // For each character, if the low byte is not equal to 0, take it. Otherwise, take the high byte. $passUtf8 = mb_convert_encoding($password, 'UCS-2LE', 'UTF-8'); - $byteChars = array(); + $byteChars = []; - for ($i = 0; $i < mb_strlen($password); $i++) { + for ($i = 0; $i < mb_strlen($password); ++$i) { $byteChars[$i] = ord(substr($passUtf8, $i * 2, 1)); if ($byteChars[$i] == 0) { @@ -135,7 +139,7 @@ public static function hashPassword($password, $algorithmName = self::ALGORITHM_ $algorithm = self::getAlgorithm($algorithmName); $generatedKey = hash($algorithm, $salt . $generatedKey, true); - for ($i = 0; $i < $spinCount; $i++) { + for ($i = 0; $i < $spinCount; ++$i) { $generatedKey = hash($algorithm, $generatedKey . pack('CCCC', $i, $i >> 8, $i >> 16, $i >> 24), true); } $generatedKey = base64_encode($generatedKey); @@ -149,6 +153,7 @@ public static function hashPassword($password, $algorithmName = self::ALGORITHM_ * Get algorithm from self::$algorithmMapping * * @param string $algorithmName + * * @return string */ private static function getAlgorithm($algorithmName) @@ -165,6 +170,7 @@ private static function getAlgorithm($algorithmName) * Returns the algorithm ID * * @param string $algorithmName + * * @return int */ public static function getAlgorithmId($algorithmName) @@ -176,6 +182,7 @@ public static function getAlgorithmId($algorithmName) * Build combined key from low-order word and high-order word * * @param array $byteChars byte array representation of password + * * @return int */ private static function buildCombinedKey($byteChars) @@ -189,10 +196,10 @@ private static function buildCombinedKey($byteChars) // For every bit in the character, starting with the least significant and progressing to (but excluding) // the most significant, if the bit is set, XOR the key’s high-order word with the corresponding word from // the Encryption Matrix - for ($i = 0; $i < $byteCharsLength; $i++) { + for ($i = 0; $i < $byteCharsLength; ++$i) { $tmp = self::$passwordMaxLength - $byteCharsLength + $i; $matrixRow = self::$encryptionMatrix[$tmp]; - for ($intBit = 0; $intBit < 7; $intBit++) { + for ($intBit = 0; $intBit < 7; ++$intBit) { if (($byteChars[$i] & (0x0001 << $intBit)) != 0) { $highOrderWord = ($highOrderWord ^ $matrixRow[$intBit]); } @@ -203,7 +210,7 @@ private static function buildCombinedKey($byteChars) // Initialize with 0 $lowOrderWord = 0; // For each character in the password, going backwards - for ($i = $byteCharsLength - 1; $i >= 0; $i--) { + for ($i = $byteCharsLength - 1; $i >= 0; --$i) { // low-order word = (((low-order word SHR 14) AND 0x0001) OR (low-order word SHL 1) AND 0x7FFF)) XOR character $lowOrderWord = (((($lowOrderWord >> 14) & 0x0001) | (($lowOrderWord << 1) & 0x7FFF)) ^ $byteChars[$i]); } @@ -218,7 +225,9 @@ private static function buildCombinedKey($byteChars) * Simulate behaviour of (signed) int32 * * @codeCoverageIgnore + * * @param int $value + * * @return int */ private static function int32($value) diff --git a/src/Common/Text.php b/src/Common/Text.php index 969b6c1..c4666f1 100644 --- a/src/Common/Text.php +++ b/src/Common/Text.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -26,7 +27,7 @@ class Text * * @var string[] */ - private static $controlCharacters = array(); + private static $controlCharacters = []; /** * Build control characters array @@ -35,8 +36,8 @@ private static function buildControlCharacters() { for ($i = 0; $i <= 19; ++$i) { if ($i != 9 && $i != 10 && $i != 13) { - $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_'; - $replace = chr($i); + $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_'; + $replace = chr($i); self::$controlCharacters[$find] = $replace; } } @@ -53,7 +54,8 @@ private static function buildControlCharacters() * So you could end up with something like _x0008_ in a string (either in a cell value () * element or in the shared string element. * - * @param string $value Value to escape + * @param string $value Value to escape + * * @return string */ public static function controlCharacterPHP2OOXML($value = '') @@ -67,8 +69,10 @@ public static function controlCharacterPHP2OOXML($value = '') /** * Return a number formatted for being integrated in xml files + * * @param float $number - * @param integer $decimals + * @param int $decimals + * * @return string */ public static function numberFormat($number, $decimals) @@ -78,24 +82,28 @@ public static function numberFormat($number, $decimals) /** * @param int $dec - * @link http://stackoverflow.com/a/7153133/2235790 + * + * @see http://stackoverflow.com/a/7153133/2235790 + * * @author velcrow + * * @return string */ public static function chr($dec) { - if ($dec<=0x7F) { + if ($dec <= 0x7F) { return chr($dec); } - if ($dec<=0x7FF) { - return chr(($dec>>6)+192).chr(($dec&63)+128); + if ($dec <= 0x7FF) { + return chr(($dec >> 6) + 192) . chr(($dec & 63) + 128); } - if ($dec<=0xFFFF) { - return chr(($dec>>12)+224).chr((($dec>>6)&63)+128).chr(($dec&63)+128); + if ($dec <= 0xFFFF) { + return chr(($dec >> 12) + 224) . chr((($dec >> 6) & 63) + 128) . chr(($dec & 63) + 128); } - if ($dec<=0x1FFFFF) { - return chr(($dec>>18)+240).chr((($dec>>12)&63)+128).chr((($dec>>6)&63)+128).chr(($dec&63)+128); + if ($dec <= 0x1FFFFF) { + return chr(($dec >> 18) + 240) . chr((($dec >> 12) & 63) + 128) . chr((($dec >> 6) & 63) + 128) . chr(($dec & 63) + 128); } + return ''; } @@ -103,6 +111,7 @@ public static function chr($dec) * Convert from OpenXML escaped control character to PHP control character * * @param string $value Value to unescape + * * @return string */ public static function controlCharacterOOXML2PHP($value = '') @@ -118,7 +127,8 @@ public static function controlCharacterOOXML2PHP($value = '') * Check if a string contains UTF-8 data * * @param string $value - * @return boolean + * + * @return bool */ public static function isUTF8($value = '') { @@ -129,6 +139,7 @@ public static function isUTF8($value = '') * Return UTF8 encoded value * * @param string $value + * * @return string */ public static function toUTF8($value = '') @@ -146,7 +157,9 @@ public static function toUTF8($value = '') * The function is splitted to reduce cyclomatic complexity * * @param string $text UTF8 text + * * @return string Unicode text + * * @since 0.11.0 */ public static function toUnicode($text) @@ -158,18 +171,20 @@ public static function toUnicode($text) * Returns unicode array from UTF8 text * * @param string $text UTF8 text + * * @return array + * * @since 0.11.0 - * @link http://www.randomchaos.com/documents/?source=php_and_unicode + * @see http://www.randomchaos.com/documents/?source=php_and_unicode */ public static function utf8ToUnicode($text) { - $unicode = array(); - $values = array(); + $unicode = []; + $values = []; $lookingFor = 1; // Gets unicode for each character - for ($i = 0; $i < strlen($text); $i++) { + for ($i = 0; $i < strlen($text); ++$i) { $thisValue = ord($text[$i]); if ($thisValue < 128) { $unicode[] = $thisValue; @@ -185,7 +200,7 @@ public static function utf8ToUnicode($text) $number = (($values[0] % 32) * 64) + ($values[1] % 64); } $unicode[] = $number; - $values = array(); + $values = []; $lookingFor = 1; } } @@ -198,9 +213,11 @@ public static function utf8ToUnicode($text) * Returns entites from unicode array * * @param array $unicode + * * @return string + * * @since 0.11.0 - * @link http://www.randomchaos.com/documents/?source=php_and_unicode + * @see http://www.randomchaos.com/documents/?source=php_and_unicode */ private static function unicodeToEntities($unicode) { @@ -219,6 +236,7 @@ private static function unicodeToEntities($unicode) * Return name without underscore for < 0.10.0 variable name compatibility * * @param string $value + * * @return string */ public static function removeUnderscorePrefix($value) diff --git a/src/Common/XMLReader.php b/src/Common/XMLReader.php index 6c720ae..ea8802a 100644 --- a/src/Common/XMLReader.php +++ b/src/Common/XMLReader.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -42,7 +43,9 @@ class XMLReader * * @param string $zipFile * @param string $xmlFile + * * @return \DOMDocument|false + * * @throws \Exception */ public function getDomFromZip($zipFile, $xmlFile) @@ -67,6 +70,7 @@ public function getDomFromZip($zipFile, $xmlFile) * Get DOMDocument from content string * * @param string $content + * * @return \DOMDocument */ public function getDomFromString($content) @@ -84,12 +88,13 @@ public function getDomFromString($content) * * @param string $path * @param \DOMElement $contextNode + * * @return \DOMNodeList */ public function getElements($path, \DOMElement $contextNode = null) { if ($this->dom === null) { - return array(); + return []; } if ($this->xpath === null) { $this->xpath = new \DOMXpath($this->dom); @@ -107,7 +112,9 @@ public function getElements($path, \DOMElement $contextNode = null) * * @param string $prefix The prefix * @param string $namespaceURI The URI of the namespace + * * @return bool true on success or false on failure + * * @throws \InvalidArgumentException If called before having loaded the DOM document */ public function registerNamespace($prefix, $namespaceURI) @@ -118,6 +125,7 @@ public function registerNamespace($prefix, $namespaceURI) if ($this->xpath === null) { $this->xpath = new \DOMXpath($this->dom); } + return $this->xpath->registerNamespace($prefix, $namespaceURI); } @@ -126,6 +134,7 @@ public function registerNamespace($prefix, $namespaceURI) * * @param string $path * @param \DOMElement $contextNode + * * @return \DOMElement|null */ public function getElement($path, \DOMElement $contextNode = null) @@ -144,6 +153,7 @@ public function getElement($path, \DOMElement $contextNode = null) * @param string $attribute * @param \DOMElement $contextNode * @param string $path + * * @return string|null */ public function getAttribute($attribute, \DOMElement $contextNode = null, $path = null) @@ -170,6 +180,7 @@ public function getAttribute($attribute, \DOMElement $contextNode = null, $path * * @param string $path * @param \DOMElement $contextNode + * * @return string|null */ public function getValue($path, \DOMElement $contextNode = null) @@ -187,7 +198,8 @@ public function getValue($path, \DOMElement $contextNode = null) * * @param string $path * @param \DOMElement $contextNode - * @return integer + * + * @return int */ public function countElements($path, \DOMElement $contextNode = null) { @@ -201,7 +213,8 @@ public function countElements($path, \DOMElement $contextNode = null) * * @param string $path * @param \DOMElement $contextNode - * @return boolean + * + * @return bool */ public function elementExists($path, \DOMElement $contextNode = null) { diff --git a/src/Common/XMLWriter.php b/src/Common/XMLWriter.php index 4440d65..c8525b9 100644 --- a/src/Common/XMLWriter.php +++ b/src/Common/XMLWriter.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -35,8 +36,8 @@ class XMLWriter extends \XMLWriter { /** Temporary storage method */ - const STORAGE_MEMORY = 1; - const STORAGE_DISK = 2; + public const STORAGE_MEMORY = 1; + public const STORAGE_DISK = 2; /** * Temporary filename @@ -87,7 +88,7 @@ public function __destruct() return; } if (PHP_OS != 'WINNT' && @unlink($this->tempFileName) === false) { - throw new \Exception('The file '.$this->tempFileName.' could not be deleted.'); + throw new \Exception('The file ' . $this->tempFileName . ' could not be deleted.'); } } @@ -103,10 +104,10 @@ public function getData() } $this->flush(); + return file_get_contents($this->tempFileName); } - /** * Write simple element and attribute(s) block * @@ -117,13 +118,14 @@ public function getData() * @param string $element * @param string|array $attributes * @param string $value + * * @return void */ public function writeElementBlock($element, $attributes, $value = null) { $this->startElement($element); if (!is_array($attributes)) { - $attributes = array($attributes => $value); + $attributes = [$attributes => $value]; } foreach ($attributes as $attribute => $value) { $this->writeAttribute($attribute, $value); @@ -138,6 +140,7 @@ public function writeElementBlock($element, $attributes, $value = null) * @param string $element * @param string $attribute * @param mixed $value + * * @return void */ public function writeElementIf($condition, $element, $attribute = null, $value = null) @@ -159,6 +162,7 @@ public function writeElementIf($condition, $element, $attribute = null, $value = * @param bool $condition * @param string $attribute * @param mixed $value + * * @return void */ public function writeAttributeIf($condition, $attribute, $value) @@ -171,6 +175,7 @@ public function writeAttributeIf($condition, $attribute, $value) /** * @param string $name * @param mixed $value + * * @return bool */ public function writeAttribute($name, $value) @@ -178,6 +183,7 @@ public function writeAttribute($name, $value) if (is_float($value)) { $value = json_encode($value); } + return parent::writeAttribute($name, $value); } } diff --git a/tests/Common/Tests/Adapter/Zip/AbstractZipAdapterTest.php b/tests/Common/Tests/Adapter/Zip/AbstractZipAdapterTest.php index c95eae7..6942a45 100644 --- a/tests/Common/Tests/Adapter/Zip/AbstractZipAdapterTest.php +++ b/tests/Common/Tests/Adapter/Zip/AbstractZipAdapterTest.php @@ -10,6 +10,7 @@ abstract class AbstractZipAdapterTest extends \PHPUnit\Framework\TestCase /** * Returns a new instance of the adapter to test + * * @return \PhpOffice\Common\Adapter\Zip\ZipInterface */ abstract protected function createAdapter(); @@ -18,9 +19,9 @@ public function setUp() { parent::setUp(); - $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR; + $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR; $this->zipTest = tempnam(sys_get_temp_dir(), 'PhpOfficeCommon'); - copy($pathResources.'Sample_01_Simple.pptx', $this->zipTest); + copy($pathResources . 'Sample_01_Simple.pptx', $this->zipTest); } public function tearDown() diff --git a/tests/Common/Tests/Adapter/Zip/PclZipAdapterTest.php b/tests/Common/Tests/Adapter/Zip/PclZipAdapterTest.php index c7535bc..138b457 100644 --- a/tests/Common/Tests/Adapter/Zip/PclZipAdapterTest.php +++ b/tests/Common/Tests/Adapter/Zip/PclZipAdapterTest.php @@ -3,7 +3,6 @@ namespace Common\Tests\Adapter\Zip; use PhpOffice\Common\Adapter\Zip\PclZipAdapter; -use PhpOffice\Common\Tests\TestHelperZip; class PclZipAdapterTest extends AbstractZipAdapterTest { diff --git a/tests/Common/Tests/Adapter/Zip/ZipArchiveAdapterTest.php b/tests/Common/Tests/Adapter/Zip/ZipArchiveAdapterTest.php index 896aa3e..91c5fa2 100644 --- a/tests/Common/Tests/Adapter/Zip/ZipArchiveAdapterTest.php +++ b/tests/Common/Tests/Adapter/Zip/ZipArchiveAdapterTest.php @@ -3,7 +3,6 @@ namespace Common\Tests\Adapter\Zip; use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter; -use PhpOffice\Common\Tests\TestHelperZip; class ZipArchiveAdapterTest extends AbstractZipAdapterTest { diff --git a/tests/Common/Tests/AutoloaderTest.php b/tests/Common/Tests/AutoloaderTest.php index ff4c792..15ef3d1 100644 --- a/tests/Common/Tests/AutoloaderTest.php +++ b/tests/Common/Tests/AutoloaderTest.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -30,7 +31,7 @@ public function testRegister() { Autoloader::register(); $this->assertContains( - array('PhpOffice\\Common\\Autoloader', 'autoload'), + ['PhpOffice\\Common\\Autoloader', 'autoload'], spl_autoload_functions() ); } diff --git a/tests/Common/Tests/DrawingTest.php b/tests/Common/Tests/DrawingTest.php index bbcf4e3..bde5590 100644 --- a/tests/Common/Tests/DrawingTest.php +++ b/tests/Common/Tests/DrawingTest.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -21,12 +22,10 @@ /** * Test class for IOFactory * - * @coversDefaultClass PhpOffice\Common\IOFactory + * @coversDefaultClass \PhpOffice\Common\IOFactory */ class DrawingTest extends \PHPUnit\Framework\TestCase { - /** - */ public function testDegreesAngle() { $value = rand(1, 100); @@ -37,8 +36,6 @@ public function testDegreesAngle() $this->assertEquals(round($value / 60000), Drawing::angleToDegrees($value)); } - /** - */ public function testPixelsCentimeters() { $value = rand(1, 100); @@ -49,32 +46,26 @@ public function testPixelsCentimeters() $this->assertEquals($value / 2.54 * Drawing::DPI_96, Drawing::centimetersToPixels($value)); } - /** - */ public function testPixelsEMU() { $value = rand(1, 100); $this->assertEquals(0, Drawing::pixelsToEmu()); - $this->assertEquals(round($value*9525), Drawing::pixelsToEmu($value)); + $this->assertEquals(round($value * 9525), Drawing::pixelsToEmu($value)); $this->assertEquals(0, Drawing::emuToPixels()); - $this->assertEquals(round($value/9525), Drawing::emuToPixels($value)); + $this->assertEquals(round($value / 9525), Drawing::emuToPixels($value)); } - /** - */ public function testPixelsPoints() { $value = rand(1, 100); $this->assertEquals(0, Drawing::pixelsToPoints()); - $this->assertEquals($value*0.67777777, Drawing::pixelsToPoints($value)); + $this->assertEquals($value * 0.67777777, Drawing::pixelsToPoints($value)); $this->assertEquals(0, Drawing::pointsToPixels()); - $this->assertEquals($value* 1.333333333, Drawing::pointsToPixels($value)); + $this->assertEquals($value * 1.333333333, Drawing::pointsToPixels($value)); } - /** - */ public function testPointsCentimeters() { $value = rand(1, 100); @@ -83,8 +74,6 @@ public function testPointsCentimeters() $this->assertEquals($value * 1.333333333 / Drawing::DPI_96 * 2.54, Drawing::pointsToCentimeters($value)); } - /** - */ public function testTwips() { $value = rand(1, 100); @@ -117,9 +106,9 @@ public function testHTML() $this->assertInternalType('array', Drawing::htmlToRGB('ABCDEF')); $this->assertCount(3, Drawing::htmlToRGB('ABCDEF')); - $this->assertEquals(array(0xAB, 0xCD, 0xEF), Drawing::htmlToRGB('ABCDEF')); - $this->assertEquals(array(0xAB, 0xCD, 0xEF), Drawing::htmlToRGB('#ABCDEF')); - $this->assertEquals(array(0xAA, 0xBB, 0xCC), Drawing::htmlToRGB('ABC')); - $this->assertEquals(array(0xAA, 0xBB, 0xCC), Drawing::htmlToRGB('#ABC')); + $this->assertEquals([0xAB, 0xCD, 0xEF], Drawing::htmlToRGB('ABCDEF')); + $this->assertEquals([0xAB, 0xCD, 0xEF], Drawing::htmlToRGB('#ABCDEF')); + $this->assertEquals([0xAA, 0xBB, 0xCC], Drawing::htmlToRGB('ABC')); + $this->assertEquals([0xAA, 0xBB, 0xCC], Drawing::htmlToRGB('#ABC')); } } diff --git a/tests/Common/Tests/FileTest.php b/tests/Common/Tests/FileTest.php index 439cc7b..9651cf7 100644 --- a/tests/Common/Tests/FileTest.php +++ b/tests/Common/Tests/FileTest.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -21,42 +22,36 @@ /** * Test class for File * - * @coversDefaultClass PhpOffice\Common\File + * @coversDefaultClass \PhpOffice\Common\File */ class FileTest extends \PHPUnit\Framework\TestCase { - /** - */ public function testFileExists() { - $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR; - $this->assertTrue(File::fileExists($pathResources.'images'.DIRECTORY_SEPARATOR.'PHPPowerPointLogo.png')); - $this->assertFalse(File::fileExists($pathResources.'images'.DIRECTORY_SEPARATOR.'PHPPowerPointLogo_404.png')); - $this->assertTrue(File::fileExists('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#[Content_Types].xml')); - $this->assertFalse(File::fileExists('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#404.xml')); - $this->assertFalse(File::fileExists('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'404.pptx#404.xml')); + $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR; + $this->assertTrue(File::fileExists($pathResources . 'images' . DIRECTORY_SEPARATOR . 'PHPPowerPointLogo.png')); + $this->assertFalse(File::fileExists($pathResources . 'images' . DIRECTORY_SEPARATOR . 'PHPPowerPointLogo_404.png')); + $this->assertTrue(File::fileExists('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#[Content_Types].xml')); + $this->assertFalse(File::fileExists('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#404.xml')); + $this->assertFalse(File::fileExists('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . '404.pptx#404.xml')); } - /** - */ public function testGetFileContents() { - $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR; - $this->assertInternalType('string', File::fileGetContents($pathResources.'images'.DIRECTORY_SEPARATOR.'PHPPowerPointLogo.png')); - $this->assertFalse(File::fileGetContents($pathResources.'images'.DIRECTORY_SEPARATOR.'PHPPowerPointLogo_404.png')); - $this->assertInternalType('string', File::fileGetContents('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#[Content_Types].xml')); - $this->assertFalse(File::fileGetContents('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#404.xml')); - $this->assertFalse(File::fileGetContents('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'404.pptx#404.xml')); + $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR; + $this->assertInternalType('string', File::fileGetContents($pathResources . 'images' . DIRECTORY_SEPARATOR . 'PHPPowerPointLogo.png')); + $this->assertFalse(File::fileGetContents($pathResources . 'images' . DIRECTORY_SEPARATOR . 'PHPPowerPointLogo_404.png')); + $this->assertInternalType('string', File::fileGetContents('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#[Content_Types].xml')); + $this->assertFalse(File::fileGetContents('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#404.xml')); + $this->assertFalse(File::fileGetContents('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . '404.pptx#404.xml')); } - /** - */ public function testRealPath() { - $pathFiles = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR; + $pathFiles = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR; - $this->assertEquals($pathFiles.'Sample_01_Simple.pptx', File::realpath($pathFiles.'Sample_01_Simple.pptx')); - $this->assertEquals('zip://'.$pathFiles.'Sample_01_Simple.pptx#[Content_Types].xml', File::realpath('zip://'.$pathFiles.'Sample_01_Simple.pptx#[Content_Types].xml')); - $this->assertEquals('zip://'.$pathFiles.'Sample_01_Simple.pptx#/[Content_Types].xml', File::realpath('zip://'.$pathFiles.'Sample_01_Simple.pptx#/rels/../[Content_Types].xml')); + $this->assertEquals($pathFiles . 'Sample_01_Simple.pptx', File::realpath($pathFiles . 'Sample_01_Simple.pptx')); + $this->assertEquals('zip://' . $pathFiles . 'Sample_01_Simple.pptx#[Content_Types].xml', File::realpath('zip://' . $pathFiles . 'Sample_01_Simple.pptx#[Content_Types].xml')); + $this->assertEquals('zip://' . $pathFiles . 'Sample_01_Simple.pptx#/[Content_Types].xml', File::realpath('zip://' . $pathFiles . 'Sample_01_Simple.pptx#/rels/../[Content_Types].xml')); } } diff --git a/tests/Common/Tests/FontTest.php b/tests/Common/Tests/FontTest.php index c661502..28ca7cc 100644 --- a/tests/Common/Tests/FontTest.php +++ b/tests/Common/Tests/FontTest.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -21,12 +22,10 @@ /** * Test class for Font * - * @coversDefaultClass PhpOffice\Common\Font + * @coversDefaultClass \PhpOffice\Common\Font */ class FontTest extends \PHPUnit\Framework\TestCase { - /** - */ public function testMath() { $value = rand(1, 100); diff --git a/tests/Common/Tests/Microsoft/PasswordEncoderTest.php b/tests/Common/Tests/Microsoft/PasswordEncoderTest.php index b796575..98b0d03 100644 --- a/tests/Common/Tests/Microsoft/PasswordEncoderTest.php +++ b/tests/Common/Tests/Microsoft/PasswordEncoderTest.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -20,6 +21,7 @@ /** * Test class for PhpOffice\Common\PasswordEncoder + * * @coversDefaultClass \PhpOffice\Common\PasswordEncoder */ class PasswordEncoderTest extends \PHPUnit\Framework\TestCase diff --git a/tests/Common/Tests/TextTest.php b/tests/Common/Tests/TextTest.php index edd65e2..f6a0ef0 100644 --- a/tests/Common/Tests/TextTest.php +++ b/tests/Common/Tests/TextTest.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -21,12 +22,10 @@ /** * Test class for Text * - * @coversDefaultClass PhpOffice\Common\Text + * @coversDefaultClass \PhpOffice\Common\Text */ class TextTest extends \PHPUnit\Framework\TestCase { - /** - */ public function testControlCharacters() { $this->assertEquals('', Text::controlCharacterPHP2OOXML()); @@ -34,7 +33,7 @@ public function testControlCharacters() $this->assertEquals('àéîöù', Text::controlCharacterPHP2OOXML('àéîöù')); $value = rand(0, 8); - $this->assertEquals('_x'.sprintf('%04s', strtoupper(dechex($value))).'_', Text::controlCharacterPHP2OOXML(chr($value))); + $this->assertEquals('_x' . sprintf('%04s', strtoupper(dechex($value))) . '_', Text::controlCharacterPHP2OOXML(chr($value))); $this->assertEquals('', Text::controlCharacterOOXML2PHP('')); $this->assertEquals(chr(0x08), Text::controlCharacterOOXML2PHP('_x0008_')); @@ -59,6 +58,7 @@ public function testChr() $this->assertEquals('🌃', Text::chr(0x1F303)); $this->assertEquals('', Text::chr(2097152)); } + /** * Is UTF8 */ diff --git a/tests/Common/Tests/XMLReaderTest.php b/tests/Common/Tests/XMLReaderTest.php index b0acde7..20107b3 100644 --- a/tests/Common/Tests/XMLReaderTest.php +++ b/tests/Common/Tests/XMLReaderTest.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2017 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -21,7 +22,7 @@ /** * Test class for XMLReader * - * @coversDefaultClass PhpOffice\Common\XMLReader + * @coversDefaultClass \PhpOffice\Common\XMLReader */ class XMLReaderTest extends \PHPUnit\Framework\TestCase { @@ -45,27 +46,27 @@ public function testDomFromString() */ public function testDomFromZip() { - $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR; + $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR; $reader = new XMLReader(); - $reader->getDomFromZip($pathResources. 'reader.zip', 'test.xml'); + $reader->getDomFromZip($pathResources . 'reader.zip', 'test.xml'); $this->assertTrue($reader->elementExists('/element/child')); - $this->assertFalse($reader->getDomFromZip($pathResources. 'reader.zip', 'non_existing_xml_file.xml')); + $this->assertFalse($reader->getDomFromZip($pathResources . 'reader.zip', 'non_existing_xml_file.xml')); } /** * Test that read from non existing archive throws exception * - * @expectedException Exception + * @expectedException \Exception */ public function testThrowsExceptionOnNonExistingArchive() { - $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR; + $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR; $reader = new XMLReader(); - $reader->getDomFromZip($pathResources. 'readers.zip', 'test.xml'); + $reader->getDomFromZip($pathResources . 'readers.zip', 'test.xml'); } /** @@ -125,7 +126,7 @@ public function testShouldParseXmlWithCustomNamespace() /** * Test that xpath fails if custom namespace is not registered * - * @expectedException InvalidArgumentException + * @expectedException \InvalidArgumentException */ public function testShouldThowExceptionIfTryingToRegisterNamespaceBeforeReadingDoc() { diff --git a/tests/Common/Tests/XMLWriterTest.php b/tests/Common/Tests/XMLWriterTest.php index 2ff2d2c..5911be6 100644 --- a/tests/Common/Tests/XMLWriterTest.php +++ b/tests/Common/Tests/XMLWriterTest.php @@ -9,7 +9,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/Common/contributors. * - * @link https://github.com/PHPOffice/Common + * @see https://github.com/PHPOffice/Common + * * @copyright 2009-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -21,27 +22,25 @@ /** * Test class for XMLWriter * - * @coversDefaultClass PhpOffice\Common\XMLWriter + * @coversDefaultClass \PhpOffice\Common\XMLWriter */ class XMLWriterTest extends \PHPUnit\Framework\TestCase { - /** - */ public function testConstruct() { // Memory $object = new XMLWriter(); $object->startElement('element'); - $object->text('AAA'); + $object->text('AAA'); $object->endElement(); - $this->assertEquals('AAA'.chr(10), $object->getData()); + $this->assertEquals('AAA' . chr(10), $object->getData()); // Disk $object = new XMLWriter(XMLWriter::STORAGE_DISK); $object->startElement('element'); - $object->text('BBB'); + $object->text('BBB'); $object->endElement(); - $this->assertEquals('BBB'.chr(10), $object->getData()); + $this->assertEquals('BBB' . chr(10), $object->getData()); } public function testWriteAttribute() @@ -65,7 +64,7 @@ public function testWriteAttributeShouldWriteFloatValueLocaleIndependent() setlocale(LC_NUMERIC, 'de_DE.UTF-8', 'de'); - $this->assertSame('1,2', (string)$value); + $this->assertSame('1,2', (string) $value); $this->assertSame('' . chr(10), $xmlWriter->getData()); } } diff --git a/tests/Common/Tests/_includes/TestHelperZip.php b/tests/Common/Tests/_includes/TestHelperZip.php index 6f7c54d..67f56ae 100644 --- a/tests/Common/Tests/_includes/TestHelperZip.php +++ b/tests/Common/Tests/_includes/TestHelperZip.php @@ -1,23 +1,25 @@ open($fileZip) !== true) { return false; } if ($oZip->statName($path) === false) { return false; } + return true; } public static function assertFileContent($fileZip, $path, $content) { - $oZip = new \ZipArchive; + $oZip = new \ZipArchive(); if ($oZip->open($fileZip) !== true) { return false; } @@ -28,6 +30,7 @@ public static function assertFileContent($fileZip, $path, $content) if ($zipFileContent != $content) { return false; } + return true; } } diff --git a/tests/Common/Tests/_includes/XmlDocument.php b/tests/Common/Tests/_includes/XmlDocument.php index 29e9d6a..35a5616 100644 --- a/tests/Common/Tests/_includes/XmlDocument.php +++ b/tests/Common/Tests/_includes/XmlDocument.php @@ -10,7 +10,8 @@ * file that was distributed with this source code. For the full list of * contributors, visit https://github.com/PHPOffice/PHPPowerPoint/contributors. * - * @link https://github.com/PHPOffice/PHPPowerPoint + * @see https://github.com/PHPOffice/PHPPowerPoint + * * @copyright 2010-2016 PHPPowerPoint contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -25,7 +26,7 @@ class XmlDocument /** * Path * - * @var string $path + * @var string */ private $path; @@ -64,6 +65,7 @@ public function __construct($path) * Get DOM from file * * @param string $file + * * @return \DOMDocument */ public function getFileDom($file = 'word/document.xml') @@ -78,6 +80,7 @@ public function getFileDom($file = 'word/document.xml') $file = $this->path . '/' . $file; $this->dom = new \DOMDocument(); $this->dom->load($file); + return $this->dom; } @@ -86,6 +89,7 @@ public function getFileDom($file = 'word/document.xml') * * @param string $path * @param string $file + * * @return \DOMNodeList */ public function getNodeList($path, $file = 'word/document.xml') @@ -106,11 +110,13 @@ public function getNodeList($path, $file = 'word/document.xml') * * @param string $path * @param string $file + * * @return \DOMElement */ public function getElement($path, $file = 'word/document.xml') { $elements = $this->getNodeList($path, $file); + return $elements->item(0); } @@ -137,10 +143,11 @@ public function getPath() /** * Get element attribute * - * @param string $path - * @param string $attribute - * @param string $file - * @return string + * @param string $path + * @param string $attribute + * @param string $file + * + * @return string */ public function getElementAttribute($path, $attribute, $file = 'word/document.xml') { @@ -150,10 +157,11 @@ public function getElementAttribute($path, $attribute, $file = 'word/document.xm /** * Get element attribute * - * @param string $path - * @param string $attribute - * @param string $file - * @return string + * @param string $path + * @param string $attribute + * @param string $file + * + * @return string */ public function attributeElementExists($path, $attribute, $file = 'word/document.xml') { @@ -163,13 +171,15 @@ public function attributeElementExists($path, $attribute, $file = 'word/document /** * Check if element exists * - * @param string $path - * @param string $file - * @return string + * @param string $path + * @param string $file + * + * @return string */ public function elementExists($path, $file = 'word/document.xml') { $nodeList = $this->getNodeList($path, $file); + return !($nodeList->length == 0); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 35fd6f1..24dd4a8 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -11,9 +11,9 @@ * * @copyright 2010-2016 PHPOffice Common contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 - * @link https://github.com/PHPOffice/Common + * + * @see https://github.com/PHPOffice/Common */ - date_default_timezone_set('UTC'); // defining base dir for tests @@ -23,14 +23,14 @@ $vendor = realpath(__DIR__ . '/../vendor'); -if (file_exists($vendor . "/autoload.php")) { - require $vendor . "/autoload.php"; +if (file_exists($vendor . '/autoload.php')) { + require $vendor . '/autoload.php'; } else { $vendor = realpath(__DIR__ . '/../../../'); - if (file_exists($vendor . "/autoload.php")) { - require $vendor . "/autoload.php"; + if (file_exists($vendor . '/autoload.php')) { + require $vendor . '/autoload.php'; } else { - throw new Exception("Unable to load dependencies"); + throw new Exception('Unable to load dependencies'); } } @@ -39,7 +39,7 @@ $prefix = 'PhpOffice\\Common\\Tests'; if (strpos($class, $prefix) === 0) { $class = str_replace('\\', DIRECTORY_SEPARATOR, $class); - $class = join(DIRECTORY_SEPARATOR, array('Common', 'Tests', '_includes')) . + $class = join(DIRECTORY_SEPARATOR, ['Common', 'Tests', '_includes']) . substr($class, strlen($prefix)); $file = __DIR__ . DIRECTORY_SEPARATOR . $class . '.php'; if (file_exists($file)) { @@ -48,5 +48,5 @@ } }); -require_once __DIR__ . "/../src/Common/Autoloader.php"; +require_once __DIR__ . '/../src/Common/Autoloader.php'; \PhpOffice\Common\Autoloader::register(); From 70ab3eed9443928ce81120026bd8a0567410ce03 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Mon, 19 Jul 2021 14:36:21 +0200 Subject: [PATCH 09/19] Added PHPMD Check --- .github/workflows/phpmd.xml.dist | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 .github/workflows/phpmd.xml.dist diff --git a/.github/workflows/phpmd.xml.dist b/.github/workflows/phpmd.xml.dist new file mode 100644 index 0000000..fca998a --- /dev/null +++ b/.github/workflows/phpmd.xml.dist @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From b11f5904a58deb0160a5e78a334d5edf17e408e1 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Tue, 20 Jul 2021 08:57:42 +0200 Subject: [PATCH 10/19] Added PHPStan Check --- composer.json | 4 +- phpstan.neon.dist | 12 ++++ src/Common/Autoloader.php | 4 +- src/Common/Drawing.php | 45 ++++++------ src/Common/File.php | 12 ++-- src/Common/Font.php | 22 +++--- src/Common/Microsoft/OLERead.php | 68 +++++++++++++------ src/Common/Microsoft/PasswordEncoder.php | 21 ++++-- src/Common/Text.php | 30 ++++---- src/Common/XMLReader.php | 57 +++++++++------- src/Common/XMLWriter.php | 4 +- .../Adapter/Zip/AbstractZipAdapterTest.php | 16 +++-- .../Tests/Adapter/Zip/PclZipAdapterTest.php | 3 +- .../Adapter/Zip/ZipArchiveAdapterTest.php | 3 +- tests/Common/Tests/AutoloaderTest.php | 4 +- tests/Common/Tests/DrawingTest.php | 22 +++--- tests/Common/Tests/FileTest.php | 18 ++--- tests/Common/Tests/FontTest.php | 2 +- .../Tests/Microsoft/PasswordEncoderTest.php | 8 +-- tests/Common/Tests/TextTest.php | 19 +++--- tests/Common/Tests/XMLReaderTest.php | 16 ++--- tests/Common/Tests/XMLWriterTest.php | 6 +- .../Common/Tests/_includes/TestHelperZip.php | 8 ++- tests/Common/Tests/_includes/XmlDocument.php | 48 +++++++------ 24 files changed, 262 insertions(+), 190 deletions(-) create mode 100644 phpstan.neon.dist diff --git a/composer.json b/composer.json index 2a75a30..203c693 100644 --- a/composer.json +++ b/composer.json @@ -22,9 +22,7 @@ "phpunit/phpunit": "^4.8.36 || ^7.0", "phpdocumentor/phpdocumentor":"2.*", "phpmd/phpmd": "2.*", - "sebastian/phpcpd": "2.*", - "phploc/phploc": "2.*", - "squizlabs/php_codesniffer": "2.*" + "phpstan/phpstan": "^0.12.88" }, "autoload": { "psr-4": { diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..71cf9ba --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,12 @@ +parameters: + level: 6 + bootstrapFiles: + - tests/bootstrap.php + paths: + - src + - tests + reportUnmatchedIgnoredErrors: false + ignoreErrors: + + ## Remove after remove ArrayObject + treatPhpDocTypesAsCertain: false diff --git a/src/Common/Autoloader.php b/src/Common/Autoloader.php index 9dff18e..9269232 100644 --- a/src/Common/Autoloader.php +++ b/src/Common/Autoloader.php @@ -30,7 +30,7 @@ class Autoloader * * @return void */ - public static function register() + public static function register(): void { spl_autoload_register([new self(), 'autoload']); } @@ -40,7 +40,7 @@ public static function register() * * @param string $class */ - public static function autoload($class) + public static function autoload(string $class): void { $prefixLength = strlen(self::NAMESPACE_PREFIX); if (0 === strncmp(self::NAMESPACE_PREFIX, $class, $prefixLength)) { diff --git a/src/Common/Drawing.php b/src/Common/Drawing.php index 8c7066a..4ec2dbf 100644 --- a/src/Common/Drawing.php +++ b/src/Common/Drawing.php @@ -17,9 +17,6 @@ namespace PhpOffice\Common; -/** - * \PhpOffice\Common\Drawing - */ class Drawing { public const DPI_96 = 96; @@ -29,9 +26,9 @@ class Drawing * * @param int $pValue Value in pixels * - * @return int + * @return float */ - public static function pixelsToEmu($pValue = 0) + public static function pixelsToEmu(int $pValue = 0): float { return round($pValue * 9525); } @@ -41,9 +38,9 @@ public static function pixelsToEmu($pValue = 0) * * @param int $pValue Value in EMU * - * @return int + * @return float */ - public static function emuToPixels($pValue = 0) + public static function emuToPixels(int $pValue = 0): float { if ($pValue == 0) { return 0; @@ -59,7 +56,7 @@ public static function emuToPixels($pValue = 0) * * @return float */ - public static function pixelsToPoints($pValue = 0) + public static function pixelsToPoints(int $pValue = 0): float { return $pValue * 0.67777777; } @@ -71,7 +68,7 @@ public static function pixelsToPoints($pValue = 0) * * @return float */ - public static function pointsToCentimeters($pValue = 0) + public static function pointsToCentimeters(int $pValue = 0): float { if ($pValue == 0) { return 0; @@ -87,7 +84,7 @@ public static function pointsToCentimeters($pValue = 0) * * @return float */ - public static function pointsToPixels($pValue = 0) + public static function pointsToPixels(int $pValue = 0): float { if ($pValue == 0) { return 0; @@ -103,7 +100,7 @@ public static function pointsToPixels($pValue = 0) * * @return float */ - public static function pixelsToCentimeters($pValue = 0) + public static function pixelsToCentimeters(int $pValue = 0): float { //return $pValue * 0.028; return ($pValue / self::DPI_96) * 2.54; @@ -116,7 +113,7 @@ public static function pixelsToCentimeters($pValue = 0) * * @return float */ - public static function centimetersToPixels($pValue = 0) + public static function centimetersToPixels(int $pValue = 0): float { if ($pValue == 0) { return 0; @@ -132,7 +129,7 @@ public static function centimetersToPixels($pValue = 0) * * @return int */ - public static function degreesToAngle($pValue = 0) + public static function degreesToAngle(int $pValue = 0): int { return (int) round($pValue * 60000); } @@ -142,9 +139,9 @@ public static function degreesToAngle($pValue = 0) * * @param int $pValue Angle * - * @return int + * @return float */ - public static function angleToDegrees($pValue = 0) + public static function angleToDegrees(int $pValue = 0): float { if ($pValue == 0) { return 0; @@ -160,7 +157,7 @@ public static function angleToDegrees($pValue = 0) * * @return float */ - public static function centimetersToTwips($pValue = 0) + public static function centimetersToTwips(int $pValue = 0): float { if ($pValue == 0) { return 0; @@ -176,7 +173,7 @@ public static function centimetersToTwips($pValue = 0) * * @return float */ - public static function twipsToCentimeters($pValue = 0) + public static function twipsToCentimeters(int $pValue = 0): float { if ($pValue == 0) { return 0; @@ -190,9 +187,9 @@ public static function twipsToCentimeters($pValue = 0) * * @param int $pValue * - * @return float + * @return int */ - public static function inchesToTwips($pValue = 0) + public static function inchesToTwips(int $pValue = 0): int { if ($pValue == 0) { return 0; @@ -208,7 +205,7 @@ public static function inchesToTwips($pValue = 0) * * @return float */ - public static function twipsToInches($pValue = 0) + public static function twipsToInches(int $pValue = 0): float { if ($pValue == 0) { return 0; @@ -224,7 +221,7 @@ public static function twipsToInches($pValue = 0) * * @return float */ - public static function twipsToPixels($pValue = 0) + public static function twipsToPixels(int $pValue = 0): float { if ($pValue == 0) { return 0; @@ -238,9 +235,9 @@ public static function twipsToPixels($pValue = 0) * * @param string $pValue HTML Color in hexadecimal * - * @return array|false Value in RGB + * @return array|null Value in RGB */ - public static function htmlToRGB($pValue) + public static function htmlToRGB(string $pValue): ?array { if ($pValue[0] == '#') { $pValue = substr($pValue, 1); @@ -251,7 +248,7 @@ public static function htmlToRGB($pValue) } elseif (strlen($pValue) == 3) { list($colorR, $colorG, $colorB) = [$pValue[0] . $pValue[0], $pValue[1] . $pValue[1], $pValue[2] . $pValue[2]]; } else { - return false; + return null; } $colorR = hexdec($colorR); diff --git a/src/Common/File.php b/src/Common/File.php index c04c395..0ed21a7 100644 --- a/src/Common/File.php +++ b/src/Common/File.php @@ -59,12 +59,12 @@ public static function fileExists($pFilename) * * @param string $pFilename Filename * - * @return string + * @return string|null */ - public static function fileGetContents($pFilename) + public static function fileGetContents(string $pFilename): ?string { if (!self::fileExists($pFilename)) { - return false; + return null; } if (strtolower(substr($pFilename, 0, 3)) == 'zip') { // Open ZIP file and verify if the file exists @@ -79,7 +79,7 @@ public static function fileGetContents($pFilename) return $returnValue; } - return false; + return null; } // Regular file contents return file_get_contents($pFilename); @@ -92,13 +92,13 @@ public static function fileGetContents($pFilename) * * @return string */ - public static function realpath($pFilename) + public static function realpath(string $pFilename): string { // Try using realpath() $returnValue = realpath($pFilename); // Found something? - if ($returnValue == '' || is_null($returnValue)) { + if (empty($returnValue)) { $pathArray = explode('/', $pFilename); while (in_array('..', $pathArray) && $pathArray[0] != '..') { $numPathArray = count($pathArray); diff --git a/src/Common/Font.php b/src/Common/Font.php index eecf611..38bc81d 100644 --- a/src/Common/Font.php +++ b/src/Common/Font.php @@ -27,9 +27,9 @@ class Font * * @param int $fontSizeInPoints Font size (in points) * - * @return int Font size (in pixels) + * @return float Font size (in pixels) */ - public static function fontSizeToPixels($fontSizeInPoints = 12) + public static function fontSizeToPixels(int $fontSizeInPoints = 12): float { return (16 / 12) * $fontSizeInPoints; } @@ -41,7 +41,7 @@ public static function fontSizeToPixels($fontSizeInPoints = 12) * * @return int Size (in pixels) */ - public static function inchSizeToPixels($sizeInInch = 1) + public static function inchSizeToPixels(int $sizeInInch = 1): int { return $sizeInInch * 96; } @@ -51,9 +51,9 @@ public static function inchSizeToPixels($sizeInInch = 1) * * @param int $sizeInCm Font size (in centimeters) * - * @return int Size (in pixels) + * @return float Size (in pixels) */ - public static function centimeterSizeToPixels($sizeInCm = 1) + public static function centimeterSizeToPixels(int $sizeInCm = 1): float { return $sizeInCm * 37.795275591; } @@ -65,7 +65,7 @@ public static function centimeterSizeToPixels($sizeInCm = 1) * * @return float */ - public static function centimeterSizeToTwips($sizeInCm = 1) + public static function centimeterSizeToTwips(int $sizeInCm = 1): float { return $sizeInCm / 2.54 * 1440; } @@ -75,9 +75,9 @@ public static function centimeterSizeToTwips($sizeInCm = 1) * * @param int $sizeInInch * - * @return float + * @return int */ - public static function inchSizeToTwips($sizeInInch = 1) + public static function inchSizeToTwips(int $sizeInInch = 1): int { return $sizeInInch * 1440; } @@ -89,7 +89,7 @@ public static function inchSizeToTwips($sizeInInch = 1) * * @return float */ - public static function pixelSizeToTwips($sizeInPixel = 1) + public static function pixelSizeToTwips(int $sizeInPixel = 1): float { return $sizeInPixel / 96 * 1440; } @@ -99,9 +99,9 @@ public static function pixelSizeToTwips($sizeInPixel = 1) * * @param int $sizeInPoint Size in point * - * @return int Size (in twips) + * @return float Size (in twips) */ - public static function pointSizeToTwips($sizeInPoint = 1) + public static function pointSizeToTwips(int $sizeInPoint = 1): float { return $sizeInPoint / 72 * 1440; } diff --git a/src/Common/Microsoft/OLERead.php b/src/Common/Microsoft/OLERead.php index b6bb69d..60d047b 100644 --- a/src/Common/Microsoft/OLERead.php +++ b/src/Common/Microsoft/OLERead.php @@ -23,6 +23,9 @@ class OLERead { + /** + * @var string + */ private $data = ''; // OLE identifier @@ -54,25 +57,55 @@ class OLERead public const START_BLOCK_POS = 0x74; public const SIZE_POS = 0x78; - public $summaryInformation = null; - public $docSummaryInfos = null; - public $powerpointDocument = null; - public $currentUser = null; - public $pictures = null; - public $rootEntry = null; + /** + * @var int|null + */ + public $summaryInformation; + /** + * @var int|null + */ + public $docSummaryInfos; + /** + * @var int|null + */ + public $powerpointDocument; + /** + * @var int|null + */ + public $currentUser; + /** + * @var int|null + */ + public $pictures; + /** + * @var int|null + */ + public $rootEntry; + /** + * @var array> + */ public $props = []; - public $smallBlockChain = null; - public $bigBlockChain = null; - public $entry = null; + /** + * @var string|null + */ + public $smallBlockChain; + /** + * @var string|null + */ + public $bigBlockChain; + /** + * @var string|null + */ + public $entry; /** * Read the file * - * @param $sFileName string Filename + * @param string $sFileName Filename * * @throws \Exception */ - public function read($sFileName) + public function read(string $sFileName): void { // Check if file exists and is readable if (!is_readable($sFileName)) { @@ -167,12 +200,8 @@ public function read($sFileName) * * @return string */ - public function getStream($stream) + public function getStream(int $stream): ?string { - if ($stream === null) { - return null; - } - $streamData = ''; if ($this->props[$stream]['size'] < self::SMALL_BLOCK_THRESHOLD) { @@ -217,7 +246,7 @@ public function getStream($stream) * * @return string Data for standard stream */ - private function readData($blID) + private function readData(int $blID): string { $block = $blID; $data = ''; @@ -234,7 +263,7 @@ private function readData($blID) /** * Read entries in the directory stream. */ - private function readPropertySets() + private function readPropertySets(): void { $offset = 0; @@ -262,7 +291,8 @@ private function readPropertySets() 'name' => $name, 'type' => $type, 'startBlock' => $startBlock, - 'size' => $size, ]; + 'size' => $size, + ]; // tmp helper to simplify checks $upName = strtoupper($name); diff --git a/src/Common/Microsoft/PasswordEncoder.php b/src/Common/Microsoft/PasswordEncoder.php index c158e35..097060b 100644 --- a/src/Common/Microsoft/PasswordEncoder.php +++ b/src/Common/Microsoft/PasswordEncoder.php @@ -37,7 +37,7 @@ class PasswordEncoder /** * Mapping between algorithm name and algorithm ID * - * @var array + * @var array> * * @see https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.writeprotection.cryptographicalgorithmsid(v=office.14).aspx */ @@ -55,6 +55,9 @@ class PasswordEncoder self::ALGORITHM_SHA_512 => [14, 'sha512'], ]; + /** + * @var array + */ private static $initialCodeArray = [ 0xE1F0, 0x1D0F, @@ -73,6 +76,9 @@ class PasswordEncoder 0x4EC3, ]; + /** + * @var array> + */ private static $encryptionMatrix = [ [0xAEFC, 0x4DD9, 0x9BB2, 0x2745, 0x4E8A, 0x9D14, 0x2A09], [0x7B61, 0xF6C2, 0xFDA5, 0xEB6B, 0xC6F7, 0x9DCF, 0x2BBF], @@ -91,6 +97,9 @@ class PasswordEncoder [0x1021, 0x2042, 0x4084, 0x8108, 0x1231, 0x2462, 0x48C4], ]; + /** + * @var int + */ private static $passwordMaxLength = 15; /** @@ -105,7 +114,7 @@ class PasswordEncoder * * @return string */ - public static function hashPassword($password, $algorithmName = self::ALGORITHM_SHA_1, $salt = null, $spinCount = 10000) + public static function hashPassword(string $password, string $algorithmName = self::ALGORITHM_SHA_1, string $salt = null, int $spinCount = 10000) { $origEncoding = mb_internal_encoding(); mb_internal_encoding('UTF-8'); @@ -156,7 +165,7 @@ public static function hashPassword($password, $algorithmName = self::ALGORITHM_ * * @return string */ - private static function getAlgorithm($algorithmName) + private static function getAlgorithm(string $algorithmName): string { $algorithm = self::$algorithmMapping[$algorithmName][1]; if ($algorithm == '') { @@ -173,7 +182,7 @@ private static function getAlgorithm($algorithmName) * * @return int */ - public static function getAlgorithmId($algorithmName) + public static function getAlgorithmId(string $algorithmName): int { return self::$algorithmMapping[$algorithmName][0]; } @@ -181,11 +190,11 @@ public static function getAlgorithmId($algorithmName) /** * Build combined key from low-order word and high-order word * - * @param array $byteChars byte array representation of password + * @param array $byteChars byte array representation of password * * @return int */ - private static function buildCombinedKey($byteChars) + private static function buildCombinedKey(array $byteChars): int { $byteCharsLength = count($byteChars); // Compute the high-order word diff --git a/src/Common/Text.php b/src/Common/Text.php index c4666f1..206efb5 100644 --- a/src/Common/Text.php +++ b/src/Common/Text.php @@ -32,7 +32,7 @@ class Text /** * Build control characters array */ - private static function buildControlCharacters() + private static function buildControlCharacters(): void { for ($i = 0; $i <= 19; ++$i) { if ($i != 9 && $i != 10 && $i != 13) { @@ -58,7 +58,7 @@ private static function buildControlCharacters() * * @return string */ - public static function controlCharacterPHP2OOXML($value = '') + public static function controlCharacterPHP2OOXML(string $value = ''): string { if (empty(self::$controlCharacters)) { self::buildControlCharacters(); @@ -75,7 +75,7 @@ public static function controlCharacterPHP2OOXML($value = '') * * @return string */ - public static function numberFormat($number, $decimals) + public static function numberFormat(float $number, int $decimals): string { return number_format($number, $decimals, '.', ''); } @@ -89,7 +89,7 @@ public static function numberFormat($number, $decimals) * * @return string */ - public static function chr($dec) + public static function chr(int $dec): string { if ($dec <= 0x7F) { return chr($dec); @@ -114,7 +114,7 @@ public static function chr($dec) * * @return string */ - public static function controlCharacterOOXML2PHP($value = '') + public static function controlCharacterOOXML2PHP(string $value = ''): string { if (empty(self::$controlCharacters)) { self::buildControlCharacters(); @@ -130,7 +130,7 @@ public static function controlCharacterOOXML2PHP($value = '') * * @return bool */ - public static function isUTF8($value = '') + public static function isUTF8(string $value = ''): bool { return is_string($value) && ($value === '' || preg_match('/^./su', $value) == 1); } @@ -138,11 +138,11 @@ public static function isUTF8($value = '') /** * Return UTF8 encoded value * - * @param string $value + * @param string|null $value * * @return string */ - public static function toUTF8($value = '') + public static function toUTF8(?string $value = ''): string { if (!is_null($value) && !self::isUTF8($value)) { $value = utf8_encode($value); @@ -162,7 +162,7 @@ public static function toUTF8($value = '') * * @since 0.11.0 */ - public static function toUnicode($text) + public static function toUnicode(string $text): string { return self::unicodeToEntities(self::utf8ToUnicode($text)); } @@ -172,12 +172,12 @@ public static function toUnicode($text) * * @param string $text UTF8 text * - * @return array + * @return array * * @since 0.11.0 * @see http://www.randomchaos.com/documents/?source=php_and_unicode */ - public static function utf8ToUnicode($text) + public static function utf8ToUnicode(string $text): array { $unicode = []; $values = []; @@ -212,14 +212,14 @@ public static function utf8ToUnicode($text) /** * Returns entites from unicode array * - * @param array $unicode + * @param array $unicode * * @return string * * @since 0.11.0 * @see http://www.randomchaos.com/documents/?source=php_and_unicode */ - private static function unicodeToEntities($unicode) + private static function unicodeToEntities(array $unicode): string { $entities = ''; @@ -235,11 +235,11 @@ private static function unicodeToEntities($unicode) /** * Return name without underscore for < 0.10.0 variable name compatibility * - * @param string $value + * @param string|null $value * * @return string */ - public static function removeUnderscorePrefix($value) + public static function removeUnderscorePrefix(?string $value): string { if (!is_null($value)) { if (substr($value, 0, 1) == '_') { diff --git a/src/Common/XMLReader.php b/src/Common/XMLReader.php index ea8802a..0d0d620 100644 --- a/src/Common/XMLReader.php +++ b/src/Common/XMLReader.php @@ -17,6 +17,13 @@ namespace PhpOffice\Common; +use DOMDocument; +use DOMElement; +use DOMNode; +use DOMNodeList; +use DOMXpath; +use ZipArchive; + /** * XML Reader wrapper * @@ -27,14 +34,14 @@ class XMLReader /** * DOMDocument object * - * @var \DOMDocument + * @var DOMDocument */ private $dom = null; /** * DOMXpath object * - * @var \DOMXpath + * @var DOMXpath */ private $xpath = null; @@ -44,7 +51,7 @@ class XMLReader * @param string $zipFile * @param string $xmlFile * - * @return \DOMDocument|false + * @return DOMDocument|false * * @throws \Exception */ @@ -54,7 +61,7 @@ public function getDomFromZip($zipFile, $xmlFile) throw new \Exception('Cannot find archive file.'); } - $zip = new \ZipArchive(); + $zip = new ZipArchive(); $zip->open($zipFile); $content = $zip->getFromName($xmlFile); $zip->close(); @@ -71,12 +78,12 @@ public function getDomFromZip($zipFile, $xmlFile) * * @param string $content * - * @return \DOMDocument + * @return DOMDocument */ public function getDomFromString($content) { $originalLibXMLEntityValue = libxml_disable_entity_loader(true); - $this->dom = new \DOMDocument(); + $this->dom = new DOMDocument(); $this->dom->loadXML($content); libxml_disable_entity_loader($originalLibXMLEntityValue); @@ -87,17 +94,17 @@ public function getDomFromString($content) * Get elements * * @param string $path - * @param \DOMElement $contextNode + * @param DOMElement $contextNode * - * @return \DOMNodeList + * @return DOMNodeList */ - public function getElements($path, \DOMElement $contextNode = null) + public function getElements(string $path, DOMElement $contextNode = null) { if ($this->dom === null) { - return []; + return new DOMNodeList; } if ($this->xpath === null) { - $this->xpath = new \DOMXpath($this->dom); + $this->xpath = new DOMXpath($this->dom); } if (is_null($contextNode)) { @@ -123,7 +130,7 @@ public function registerNamespace($prefix, $namespaceURI) throw new \InvalidArgumentException('Dom needs to be loaded before registering a namespace'); } if ($this->xpath === null) { - $this->xpath = new \DOMXpath($this->dom); + $this->xpath = new DOMXpath($this->dom); } return $this->xpath->registerNamespace($prefix, $namespaceURI); @@ -133,15 +140,15 @@ public function registerNamespace($prefix, $namespaceURI) * Get element * * @param string $path - * @param \DOMElement $contextNode + * @param DOMElement $contextNode * - * @return \DOMElement|null + * @return DOMElement|null */ - public function getElement($path, \DOMElement $contextNode = null) + public function getElement($path, DOMElement $contextNode = null): ?DOMElement { $elements = $this->getElements($path, $contextNode); if ($elements->length > 0) { - return $elements->item(0); + return $elements->item(0) instanceof DOMElement ? $elements->item(0) : null; } return null; @@ -151,18 +158,18 @@ public function getElement($path, \DOMElement $contextNode = null) * Get element attribute * * @param string $attribute - * @param \DOMElement $contextNode + * @param DOMElement $contextNode * @param string $path * * @return string|null */ - public function getAttribute($attribute, \DOMElement $contextNode = null, $path = null) + public function getAttribute($attribute, DOMElement $contextNode = null, $path = null) { $return = null; if ($path !== null) { $elements = $this->getElements($path, $contextNode); if ($elements->length > 0) { - /** @var \DOMElement $node Type hint */ + /** @var DOMElement $node Type hint */ $node = $elements->item(0); $return = $node->getAttribute($attribute); } @@ -179,11 +186,11 @@ public function getAttribute($attribute, \DOMElement $contextNode = null, $path * Get element value * * @param string $path - * @param \DOMElement $contextNode + * @param DOMElement $contextNode * * @return string|null */ - public function getValue($path, \DOMElement $contextNode = null) + public function getValue($path, DOMElement $contextNode = null) { $elements = $this->getElements($path, $contextNode); if ($elements->length > 0) { @@ -197,11 +204,11 @@ public function getValue($path, \DOMElement $contextNode = null) * Count elements * * @param string $path - * @param \DOMElement $contextNode + * @param DOMElement $contextNode * * @return int */ - public function countElements($path, \DOMElement $contextNode = null) + public function countElements($path, DOMElement $contextNode = null) { $elements = $this->getElements($path, $contextNode); @@ -212,11 +219,11 @@ public function countElements($path, \DOMElement $contextNode = null) * Element exists * * @param string $path - * @param \DOMElement $contextNode + * @param DOMElement $contextNode * * @return bool */ - public function elementExists($path, \DOMElement $contextNode = null) + public function elementExists($path, DOMElement $contextNode = null) { return $this->getElements($path, $contextNode)->length > 0; } diff --git a/src/Common/XMLWriter.php b/src/Common/XMLWriter.php index c8525b9..1c4b619 100644 --- a/src/Common/XMLWriter.php +++ b/src/Common/XMLWriter.php @@ -116,12 +116,12 @@ public function getData() * 2. If not, then it's a simple attribute-value pair * * @param string $element - * @param string|array $attributes + * @param string|array $attributes * @param string $value * * @return void */ - public function writeElementBlock($element, $attributes, $value = null) + public function writeElementBlock(string $element, $attributes, string $value = null) { $this->startElement($element); if (!is_array($attributes)) { diff --git a/tests/Common/Tests/Adapter/Zip/AbstractZipAdapterTest.php b/tests/Common/Tests/Adapter/Zip/AbstractZipAdapterTest.php index 6942a45..3387cf4 100644 --- a/tests/Common/Tests/Adapter/Zip/AbstractZipAdapterTest.php +++ b/tests/Common/Tests/Adapter/Zip/AbstractZipAdapterTest.php @@ -2,10 +2,14 @@ namespace Common\Tests\Adapter\Zip; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\Tests\TestHelperZip; abstract class AbstractZipAdapterTest extends \PHPUnit\Framework\TestCase { + /** + * @var string + */ protected $zipTest; /** @@ -13,9 +17,9 @@ abstract class AbstractZipAdapterTest extends \PHPUnit\Framework\TestCase * * @return \PhpOffice\Common\Adapter\Zip\ZipInterface */ - abstract protected function createAdapter(); + abstract protected function createAdapter(): ZipInterface; - public function setUp() + public function setUp(): void { parent::setUp(); @@ -24,7 +28,7 @@ public function setUp() copy($pathResources . 'Sample_01_Simple.pptx', $this->zipTest); } - public function tearDown() + public function tearDown(): void { parent::tearDown(); @@ -33,20 +37,20 @@ public function tearDown() } } - public function testOpen() + public function testOpen(): void { $adapter = $this->createAdapter(); $this->assertSame($adapter, $adapter->open($this->zipTest)); } - public function testClose() + public function testClose(): void { $adapter = $this->createAdapter(); $adapter->open($this->zipTest); $this->assertSame($adapter, $adapter->close()); } - public function testAddFromString() + public function testAddFromString(): void { $expectedPath = 'file.test'; $expectedContent = 'Content'; diff --git a/tests/Common/Tests/Adapter/Zip/PclZipAdapterTest.php b/tests/Common/Tests/Adapter/Zip/PclZipAdapterTest.php index 138b457..48b40ed 100644 --- a/tests/Common/Tests/Adapter/Zip/PclZipAdapterTest.php +++ b/tests/Common/Tests/Adapter/Zip/PclZipAdapterTest.php @@ -3,10 +3,11 @@ namespace Common\Tests\Adapter\Zip; use PhpOffice\Common\Adapter\Zip\PclZipAdapter; +use PhpOffice\Common\Adapter\Zip\ZipInterface; class PclZipAdapterTest extends AbstractZipAdapterTest { - protected function createAdapter() + protected function createAdapter(): ZipInterface { return new PclZipAdapter(); } diff --git a/tests/Common/Tests/Adapter/Zip/ZipArchiveAdapterTest.php b/tests/Common/Tests/Adapter/Zip/ZipArchiveAdapterTest.php index 91c5fa2..01518ad 100644 --- a/tests/Common/Tests/Adapter/Zip/ZipArchiveAdapterTest.php +++ b/tests/Common/Tests/Adapter/Zip/ZipArchiveAdapterTest.php @@ -3,10 +3,11 @@ namespace Common\Tests\Adapter\Zip; use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter; +use PhpOffice\Common\Adapter\Zip\ZipInterface; class ZipArchiveAdapterTest extends AbstractZipAdapterTest { - protected function createAdapter() + protected function createAdapter(): ZipInterface { return new ZipArchiveAdapter(); } diff --git a/tests/Common/Tests/AutoloaderTest.php b/tests/Common/Tests/AutoloaderTest.php index 15ef3d1..d20458a 100644 --- a/tests/Common/Tests/AutoloaderTest.php +++ b/tests/Common/Tests/AutoloaderTest.php @@ -27,7 +27,7 @@ class AutoloaderTest extends \PHPUnit\Framework\TestCase /** * Register */ - public function testRegister() + public function testRegister(): void { Autoloader::register(); $this->assertContains( @@ -39,7 +39,7 @@ public function testRegister() /** * Autoload */ - public function testAutoload() + public function testAutoload(): void { $declared = get_declared_classes(); $declaredCount = count($declared); diff --git a/tests/Common/Tests/DrawingTest.php b/tests/Common/Tests/DrawingTest.php index bde5590..b727440 100644 --- a/tests/Common/Tests/DrawingTest.php +++ b/tests/Common/Tests/DrawingTest.php @@ -26,7 +26,7 @@ */ class DrawingTest extends \PHPUnit\Framework\TestCase { - public function testDegreesAngle() + public function testDegreesAngle(): void { $value = rand(1, 100); @@ -36,7 +36,7 @@ public function testDegreesAngle() $this->assertEquals(round($value / 60000), Drawing::angleToDegrees($value)); } - public function testPixelsCentimeters() + public function testPixelsCentimeters(): void { $value = rand(1, 100); @@ -46,7 +46,7 @@ public function testPixelsCentimeters() $this->assertEquals($value / 2.54 * Drawing::DPI_96, Drawing::centimetersToPixels($value)); } - public function testPixelsEMU() + public function testPixelsEMU(): void { $value = rand(1, 100); @@ -56,7 +56,7 @@ public function testPixelsEMU() $this->assertEquals(round($value / 9525), Drawing::emuToPixels($value)); } - public function testPixelsPoints() + public function testPixelsPoints(): void { $value = rand(1, 100); @@ -66,7 +66,7 @@ public function testPixelsPoints() $this->assertEquals($value * 1.333333333, Drawing::pointsToPixels($value)); } - public function testPointsCentimeters() + public function testPointsCentimeters(): void { $value = rand(1, 100); @@ -74,7 +74,7 @@ public function testPointsCentimeters() $this->assertEquals($value * 1.333333333 / Drawing::DPI_96 * 2.54, Drawing::pointsToCentimeters($value)); } - public function testTwips() + public function testTwips(): void { $value = rand(1, 100); @@ -97,12 +97,12 @@ public function testTwips() $this->assertEquals(round($value / 15.873984), Drawing::twipsToPixels($value)); } - public function testHTML() + public function testHTML(): void { - $this->assertFalse(Drawing::htmlToRGB('0')); - $this->assertFalse(Drawing::htmlToRGB('00')); - $this->assertFalse(Drawing::htmlToRGB('0000')); - $this->assertFalse(Drawing::htmlToRGB('00000')); + $this->assertNull(Drawing::htmlToRGB('0')); + $this->assertNull(Drawing::htmlToRGB('00')); + $this->assertNull(Drawing::htmlToRGB('0000')); + $this->assertNull(Drawing::htmlToRGB('00000')); $this->assertInternalType('array', Drawing::htmlToRGB('ABCDEF')); $this->assertCount(3, Drawing::htmlToRGB('ABCDEF')); diff --git a/tests/Common/Tests/FileTest.php b/tests/Common/Tests/FileTest.php index 9651cf7..395bf26 100644 --- a/tests/Common/Tests/FileTest.php +++ b/tests/Common/Tests/FileTest.php @@ -26,7 +26,7 @@ */ class FileTest extends \PHPUnit\Framework\TestCase { - public function testFileExists() + public function testFileExists(): void { $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR; $this->assertTrue(File::fileExists($pathResources . 'images' . DIRECTORY_SEPARATOR . 'PHPPowerPointLogo.png')); @@ -36,22 +36,24 @@ public function testFileExists() $this->assertFalse(File::fileExists('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . '404.pptx#404.xml')); } - public function testGetFileContents() + public function testGetFileContents(): void { $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR; $this->assertInternalType('string', File::fileGetContents($pathResources . 'images' . DIRECTORY_SEPARATOR . 'PHPPowerPointLogo.png')); - $this->assertFalse(File::fileGetContents($pathResources . 'images' . DIRECTORY_SEPARATOR . 'PHPPowerPointLogo_404.png')); + $this->assertNull(File::fileGetContents($pathResources . 'images' . DIRECTORY_SEPARATOR . 'PHPPowerPointLogo_404.png')); $this->assertInternalType('string', File::fileGetContents('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#[Content_Types].xml')); - $this->assertFalse(File::fileGetContents('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#404.xml')); - $this->assertFalse(File::fileGetContents('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . '404.pptx#404.xml')); + $this->assertNull(File::fileGetContents('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#404.xml')); + $this->assertNull(File::fileGetContents('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . '404.pptx#404.xml')); } - public function testRealPath() + public function testRealPath(): void { $pathFiles = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR; - $this->assertEquals($pathFiles . 'Sample_01_Simple.pptx', File::realpath($pathFiles . 'Sample_01_Simple.pptx')); - $this->assertEquals('zip://' . $pathFiles . 'Sample_01_Simple.pptx#[Content_Types].xml', File::realpath('zip://' . $pathFiles . 'Sample_01_Simple.pptx#[Content_Types].xml')); + $this->assertEquals( + 'zip://' . $pathFiles . 'Sample_01_Simple.pptx#[Content_Types].xml', + File::realpath('zip://' . $pathFiles . 'Sample_01_Simple.pptx#[Content_Types].xml') + ); $this->assertEquals('zip://' . $pathFiles . 'Sample_01_Simple.pptx#/[Content_Types].xml', File::realpath('zip://' . $pathFiles . 'Sample_01_Simple.pptx#/rels/../[Content_Types].xml')); } } diff --git a/tests/Common/Tests/FontTest.php b/tests/Common/Tests/FontTest.php index 28ca7cc..d03e3e2 100644 --- a/tests/Common/Tests/FontTest.php +++ b/tests/Common/Tests/FontTest.php @@ -26,7 +26,7 @@ */ class FontTest extends \PHPUnit\Framework\TestCase { - public function testMath() + public function testMath(): void { $value = rand(1, 100); $this->assertEquals(16, Font::fontSizeToPixels()); diff --git a/tests/Common/Tests/Microsoft/PasswordEncoderTest.php b/tests/Common/Tests/Microsoft/PasswordEncoderTest.php index 98b0d03..538aa4e 100644 --- a/tests/Common/Tests/Microsoft/PasswordEncoderTest.php +++ b/tests/Common/Tests/Microsoft/PasswordEncoderTest.php @@ -29,7 +29,7 @@ class PasswordEncoderTest extends \PHPUnit\Framework\TestCase /** * Test that a password can be hashed without specifying any additional parameters */ - public function testEncodePassword() + public function testEncodePassword(): void { //given $password = 'test'; @@ -44,7 +44,7 @@ public function testEncodePassword() /** * Test that a password can be hashed with a custom salt */ - public function testEncodePasswordWithSalt() + public function testEncodePasswordWithSalt(): void { //given $password = 'test'; @@ -60,7 +60,7 @@ public function testEncodePasswordWithSalt() /** * Test that the encoder falls back on SHA-1 if a non supported algorithm is given */ - public function testDefaultsToSha1IfUnsupportedAlgorithm() + public function testDefaultsToSha1IfUnsupportedAlgorithm(): void { //given $password = 'test'; @@ -76,7 +76,7 @@ public function testDefaultsToSha1IfUnsupportedAlgorithm() /** * Test that the encoder falls back on SHA-1 if a non supported algorithm is given */ - public function testEncodePasswordWithNullAsciiCodeInPassword() + public function testEncodePasswordWithNullAsciiCodeInPassword(): void { //given $password = 'test' . chr(0); diff --git a/tests/Common/Tests/TextTest.php b/tests/Common/Tests/TextTest.php index f6a0ef0..3ba94d7 100644 --- a/tests/Common/Tests/TextTest.php +++ b/tests/Common/Tests/TextTest.php @@ -18,15 +18,16 @@ namespace PhpOffice\Common\Tests; use PhpOffice\Common\Text; +use PHPUnit\Framework\TestCase; /** * Test class for Text * * @coversDefaultClass \PhpOffice\Common\Text */ -class TextTest extends \PHPUnit\Framework\TestCase +class TextTest extends TestCase { - public function testControlCharacters() + public function testControlCharacters(): void { $this->assertEquals('', Text::controlCharacterPHP2OOXML()); $this->assertEquals('aeiou', Text::controlCharacterPHP2OOXML('aeiou')); @@ -39,14 +40,14 @@ public function testControlCharacters() $this->assertEquals(chr(0x08), Text::controlCharacterOOXML2PHP('_x0008_')); } - public function testNumberFormat() + public function testNumberFormat(): void { - $this->assertEquals('2.1', Text::numberFormat('2.06', 1)); - $this->assertEquals('2.1', Text::numberFormat('2.12', 1)); + $this->assertEquals('2.1', Text::numberFormat(2.06, 1)); + $this->assertEquals('2.1', Text::numberFormat(2.12, 1)); $this->assertEquals('1234.0', Text::numberFormat(1234, 1)); } - public function testChr() + public function testChr(): void { $this->assertEquals('A', Text::chr(65)); $this->assertEquals('A', Text::chr(0x41)); @@ -62,7 +63,7 @@ public function testChr() /** * Is UTF8 */ - public function testIsUTF8() + public function testIsUTF8(): void { $this->assertTrue(Text::isUTF8('')); $this->assertTrue(Text::isUTF8('éééé')); @@ -72,7 +73,7 @@ public function testIsUTF8() /** * Test unicode conversion */ - public function testToUnicode() + public function testToUnicode(): void { $this->assertEquals('a', Text::toUnicode('a')); $this->assertEquals('\uc0{\u8364}', Text::toUnicode('€')); @@ -82,7 +83,7 @@ public function testToUnicode() /** * Test remove underscore prefix */ - public function testRemoveUnderscorePrefix() + public function testRemoveUnderscorePrefix(): void { $this->assertEquals('item', Text::removeUnderscorePrefix('_item')); } diff --git a/tests/Common/Tests/XMLReaderTest.php b/tests/Common/Tests/XMLReaderTest.php index 20107b3..b5a345c 100644 --- a/tests/Common/Tests/XMLReaderTest.php +++ b/tests/Common/Tests/XMLReaderTest.php @@ -29,7 +29,7 @@ class XMLReaderTest extends \PHPUnit\Framework\TestCase /** * Test reading XML from string */ - public function testDomFromString() + public function testDomFromString(): void { $reader = new XMLReader(); $reader->getDomFromString('AAA'); @@ -44,7 +44,7 @@ public function testDomFromString() /** * Test reading XML from zip */ - public function testDomFromZip() + public function testDomFromZip(): void { $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR; @@ -61,7 +61,7 @@ public function testDomFromZip() * * @expectedException \Exception */ - public function testThrowsExceptionOnNonExistingArchive() + public function testThrowsExceptionOnNonExistingArchive(): void { $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR; @@ -72,7 +72,7 @@ public function testThrowsExceptionOnNonExistingArchive() /** * Test elements count */ - public function testCountElements() + public function testCountElements(): void { $reader = new XMLReader(); $reader->getDomFromString('AAABBB'); @@ -83,7 +83,7 @@ public function testCountElements() /** * Test read non existing elements */ - public function testReturnNullOnNonExistingNode() + public function testReturnNullOnNonExistingNode(): void { $reader = new XMLReader(); $this->assertEmpty($reader->getElements('/element/children')); @@ -96,7 +96,7 @@ public function testReturnNullOnNonExistingNode() /** * Test that xpath fails if custom namespace is not registered */ - public function testShouldThrowExceptionIfNamespaceIsNotKnown() + public function testShouldThrowExceptionIfNamespaceIsNotKnown(): void { try { $reader = new XMLReader(); @@ -113,7 +113,7 @@ public function testShouldThrowExceptionIfNamespaceIsNotKnown() /** * Test reading XML with manually registered namespace */ - public function testShouldParseXmlWithCustomNamespace() + public function testShouldParseXmlWithCustomNamespace(): void { $reader = new XMLReader(); $reader->getDomFromString('AAA'); @@ -128,7 +128,7 @@ public function testShouldParseXmlWithCustomNamespace() * * @expectedException \InvalidArgumentException */ - public function testShouldThowExceptionIfTryingToRegisterNamespaceBeforeReadingDoc() + public function testShouldThowExceptionIfTryingToRegisterNamespaceBeforeReadingDoc(): void { $reader = new XMLReader(); $reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace'); diff --git a/tests/Common/Tests/XMLWriterTest.php b/tests/Common/Tests/XMLWriterTest.php index 5911be6..c8ba235 100644 --- a/tests/Common/Tests/XMLWriterTest.php +++ b/tests/Common/Tests/XMLWriterTest.php @@ -26,7 +26,7 @@ */ class XMLWriterTest extends \PHPUnit\Framework\TestCase { - public function testConstruct() + public function testConstruct(): void { // Memory $object = new XMLWriter(); @@ -43,7 +43,7 @@ public function testConstruct() $this->assertEquals('BBB' . chr(10), $object->getData()); } - public function testWriteAttribute() + public function testWriteAttribute(): void { $xmlWriter = new XMLWriter(); $xmlWriter->startElement('element'); @@ -53,7 +53,7 @@ public function testWriteAttribute() $this->assertSame('' . chr(10), $xmlWriter->getData()); } - public function testWriteAttributeShouldWriteFloatValueLocaleIndependent() + public function testWriteAttributeShouldWriteFloatValueLocaleIndependent(): void { $value = 1.2; diff --git a/tests/Common/Tests/_includes/TestHelperZip.php b/tests/Common/Tests/_includes/TestHelperZip.php index 67f56ae..df59087 100644 --- a/tests/Common/Tests/_includes/TestHelperZip.php +++ b/tests/Common/Tests/_includes/TestHelperZip.php @@ -2,11 +2,13 @@ namespace PhpOffice\Common\Tests; +use ZipArchive; + class TestHelperZip { - public static function assertFileExists($fileZip, $path) + public static function assertFileExists(string $fileZip, string $path): bool { - $oZip = new \ZipArchive(); + $oZip = new ZipArchive(); if ($oZip->open($fileZip) !== true) { return false; } @@ -17,7 +19,7 @@ public static function assertFileExists($fileZip, $path) return true; } - public static function assertFileContent($fileZip, $path, $content) + public static function assertFileContent(string $fileZip, string $path, string $content): bool { $oZip = new \ZipArchive(); if ($oZip->open($fileZip) !== true) { diff --git a/tests/Common/Tests/_includes/XmlDocument.php b/tests/Common/Tests/_includes/XmlDocument.php index 35a5616..aa5a09e 100644 --- a/tests/Common/Tests/_includes/XmlDocument.php +++ b/tests/Common/Tests/_includes/XmlDocument.php @@ -18,6 +18,12 @@ namespace PhpOffice\PhpPowerpoint\Tests; +use DOMDocument; +use DOMElement; +use DOMNode; +use DOMNodeList; +use DOMXpath; + /** * DOM wrapper class */ @@ -33,14 +39,14 @@ class XmlDocument /** * DOMDocument object * - * @var \DOMDocument + * @var DOMDocument */ private $dom; /** * DOMXpath object * - * @var \DOMXpath + * @var DOMXpath|null */ private $xpath; @@ -56,7 +62,7 @@ class XmlDocument * * @param string $path */ - public function __construct($path) + public function __construct(string $path) { $this->path = realpath($path); } @@ -66,9 +72,9 @@ public function __construct($path) * * @param string $file * - * @return \DOMDocument + * @return DOMDocument */ - public function getFileDom($file = 'word/document.xml') + public function getFileDom(string $file = 'word/document.xml'): DOMDocument { if (null !== $this->dom && $file === $this->file) { return $this->dom; @@ -78,7 +84,7 @@ public function getFileDom($file = 'word/document.xml') $this->file = $file; $file = $this->path . '/' . $file; - $this->dom = new \DOMDocument(); + $this->dom = new DOMDocument(); $this->dom->load($file); return $this->dom; @@ -90,16 +96,16 @@ public function getFileDom($file = 'word/document.xml') * @param string $path * @param string $file * - * @return \DOMNodeList + * @return DOMNodeList */ - public function getNodeList($path, $file = 'word/document.xml') + public function getNodeList(string $path, string $file = 'word/document.xml'): DOMNodeList { if ($this->dom === null || $file !== $this->file) { $this->getFileDom($file); } if (null === $this->xpath) { - $this->xpath = new \DOMXpath($this->dom); + $this->xpath = new DOMXpath($this->dom); } return $this->xpath->query($path); @@ -111,9 +117,9 @@ public function getNodeList($path, $file = 'word/document.xml') * @param string $path * @param string $file * - * @return \DOMElement + * @return DOMNode */ - public function getElement($path, $file = 'word/document.xml') + public function getElement(string $path, string $file = 'word/document.xml'): DOMNode { $elements = $this->getNodeList($path, $file); @@ -125,7 +131,7 @@ public function getElement($path, $file = 'word/document.xml') * * @return string */ - public function getFile() + public function getFile(): string { return $this->file; } @@ -135,7 +141,7 @@ public function getFile() * * @return string */ - public function getPath() + public function getPath(): string { return $this->path; } @@ -149,9 +155,10 @@ public function getPath() * * @return string */ - public function getElementAttribute($path, $attribute, $file = 'word/document.xml') + public function getElementAttribute(string $path, string $attribute, string $file = 'word/document.xml'): string { - return $this->getElement($path, $file)->getAttribute($attribute); + $element = $this->getElement($path, $file); + return $element instanceof DOMElement ? $element->getAttribute($attribute) : ''; } /** @@ -161,11 +168,12 @@ public function getElementAttribute($path, $attribute, $file = 'word/document.xm * @param string $attribute * @param string $file * - * @return string + * @return bool */ - public function attributeElementExists($path, $attribute, $file = 'word/document.xml') + public function attributeElementExists(string $path, string $attribute, string $file = 'word/document.xml'): bool { - return $this->getElement($path, $file)->hasAttribute($attribute); + $element = $this->getElement($path, $file); + return $element instanceof DOMElement ? $element->hasAttribute($attribute) : false; } /** @@ -174,9 +182,9 @@ public function attributeElementExists($path, $attribute, $file = 'word/document * @param string $path * @param string $file * - * @return string + * @return bool */ - public function elementExists($path, $file = 'word/document.xml') + public function elementExists(string $path, string $file = 'word/document.xml'): bool { $nodeList = $this->getNodeList($path, $file); From cf7bc4f1a62a239f12445ca40fcc101e7e7968d5 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Tue, 20 Jul 2021 08:57:57 +0200 Subject: [PATCH 11/19] Added Github Action (PHP) --- .github/workflows/php.yml | 98 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 .github/workflows/php.yml diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml new file mode 100644 index 0000000..ef3b624 --- /dev/null +++ b/.github/workflows/php.yml @@ -0,0 +1,98 @@ +name: PHPPresentation +on: [push, pull_request] +jobs: + php-cs-fixer: + name: PHP CS Fixer + runs-on: ubuntu-latest + steps: + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + extensions: mbstring, intl, gd, xml, dom, json, fileinfo, curl, zip, iconv + - uses: actions/checkout@v2 + + - name: Validate composer config + run: composer validate --strict + + - name: Composer Install + run: composer global require friendsofphp/php-cs-fixer + + - name: Add environment path + run: export PATH="$PATH:$HOME/.composer/vendor/bin" + + - name: Run PHPCSFixer + run: php-cs-fixer fix --dry-run --diff + + phpmd: + name: PHP Mess Detector + runs-on: ubuntu-latest + steps: + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + extensions: gd, xml, zip + - uses: actions/checkout@v2 + + - name: Composer Install + run: composer install --ansi --prefer-dist --no-interaction --no-progress + + - name: Run phpmd + run: ./vendor/bin/phpmd src/,tests/ text ./phpmd.xml.dist + + phpstan: + name: PHP Static Analysis + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php: ['7.1', '7.2', '7.3', '7.4', '8.0'] #, '8.1' + steps: + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: gd, xml, zip + - uses: actions/checkout@v2 + + - name: Composer Install + run: composer install --ansi --prefer-dist --no-interaction --no-progress + + - name: Run phpstan + run: ./vendor/bin/phpstan analyse -c phpstan.neon.dist + + phpunit: + name: PHPUnit + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php: ['7.1', '7.2', '7.3', '7.4', '8.0'] #, '8.1' + steps: + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: gd, xml, zip + coverage: xdebug + + - name: Generate Locale (for tests) + run: sudo locale-gen de_DE.UTF-8 + + - uses: actions/checkout@v2 + + - name: Composer Install + run: composer install --ansi --prefer-dist --no-interaction --no-progress + + - name: Run phpunit + run: ./vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover build/clover.xml + + - name: Upload coverage results to Coveralls + if: matrix.php == '7.3' + env: + COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.4.3/php-coveralls.phar + chmod +x php-coveralls.phar + php php-coveralls.phar --coverage_clover=build/clover.xml --json_path=build/coveralls-upload.json -vvv From 3bf178763e1733579cfff0b470301a76ea767c62 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Tue, 20 Jul 2021 09:04:00 +0200 Subject: [PATCH 12/19] Fixed CI --- .github/workflows/php.yml | 2 +- .gitignore | 32 ++++---------------- composer.json | 5 ++- phpunit.xml.dist | 3 +- src/Common/XMLReader.php | 7 ++--- tests/Common/Tests/DrawingTest.php | 2 +- tests/Common/Tests/FileTest.php | 4 +-- tests/Common/Tests/XMLReaderTest.php | 17 +++++++---- tests/Common/Tests/XMLWriterTest.php | 10 ++++-- tests/Common/Tests/_includes/XmlDocument.php | 4 ++- 10 files changed, 38 insertions(+), 48 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index ef3b624..8c84763 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -78,7 +78,7 @@ jobs: coverage: xdebug - name: Generate Locale (for tests) - run: sudo locale-gen de_DE.UTF-8 + run: sudo locale-gen de_DE.UTF-8 && sudo update-locale - uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index 213b97b..394d588 100644 --- a/.gitignore +++ b/.gitignore @@ -1,27 +1,7 @@ -.DS_Store -._* -.Spotlight-V100 -.Trashes -Thumbs.db -Desktop.ini -.idea -build/ -phpunit.xml -composer.phar -vendor -*.settings -*.project -*.buildpath - -/samples/results -/phpunit.bat -/todo.txt -/samples/Sample_00_Test.php -/samples/#47 -/samples/#70 -/samples/#71 -/samples/Github_*.* -/samples/#83/*.lnk +## PHPCSFixer +/.php-cs-fixer.cache +## PHPUnit +/.phpunit.result.cache +## Dependencies /composer.lock - -/.php-cs-fixer.cache \ No newline at end of file +/vendor/ \ No newline at end of file diff --git a/composer.json b/composer.json index 203c693..613c402 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "keywords": ["PHP","Office","Common","component"], "homepage": "http://phpoffice.github.io", "type": "library", - "license": "LGPL", + "license": "LGPL-3.0-only", "authors": [ { "name": "Mark Baker" @@ -19,8 +19,7 @@ "pclzip/pclzip": "^2.8" }, "require-dev": { - "phpunit/phpunit": "^4.8.36 || ^7.0", - "phpdocumentor/phpdocumentor":"2.*", + "phpunit/phpunit": ">=7.0", "phpmd/phpmd": "2.*", "phpstan/phpstan": "^0.12.88" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7b09d34..05dc372 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -18,7 +18,6 @@ - - + \ No newline at end of file diff --git a/src/Common/XMLReader.php b/src/Common/XMLReader.php index 0d0d620..9d3f117 100644 --- a/src/Common/XMLReader.php +++ b/src/Common/XMLReader.php @@ -19,7 +19,6 @@ use DOMDocument; use DOMElement; -use DOMNode; use DOMNodeList; use DOMXpath; use ZipArchive; @@ -55,7 +54,7 @@ class XMLReader * * @throws \Exception */ - public function getDomFromZip($zipFile, $xmlFile) + public function getDomFromZip(string $zipFile, string $xmlFile) { if (file_exists($zipFile) === false) { throw new \Exception('Cannot find archive file.'); @@ -80,7 +79,7 @@ public function getDomFromZip($zipFile, $xmlFile) * * @return DOMDocument */ - public function getDomFromString($content) + public function getDomFromString(string $content) { $originalLibXMLEntityValue = libxml_disable_entity_loader(true); $this->dom = new DOMDocument(); @@ -101,7 +100,7 @@ public function getDomFromString($content) public function getElements(string $path, DOMElement $contextNode = null) { if ($this->dom === null) { - return new DOMNodeList; + return new DOMNodeList(); } if ($this->xpath === null) { $this->xpath = new DOMXpath($this->dom); diff --git a/tests/Common/Tests/DrawingTest.php b/tests/Common/Tests/DrawingTest.php index b727440..7f902de 100644 --- a/tests/Common/Tests/DrawingTest.php +++ b/tests/Common/Tests/DrawingTest.php @@ -104,7 +104,7 @@ public function testHTML(): void $this->assertNull(Drawing::htmlToRGB('0000')); $this->assertNull(Drawing::htmlToRGB('00000')); - $this->assertInternalType('array', Drawing::htmlToRGB('ABCDEF')); + $this->assertIsArray(Drawing::htmlToRGB('ABCDEF')); $this->assertCount(3, Drawing::htmlToRGB('ABCDEF')); $this->assertEquals([0xAB, 0xCD, 0xEF], Drawing::htmlToRGB('ABCDEF')); $this->assertEquals([0xAB, 0xCD, 0xEF], Drawing::htmlToRGB('#ABCDEF')); diff --git a/tests/Common/Tests/FileTest.php b/tests/Common/Tests/FileTest.php index 395bf26..91fe57a 100644 --- a/tests/Common/Tests/FileTest.php +++ b/tests/Common/Tests/FileTest.php @@ -39,9 +39,9 @@ public function testFileExists(): void public function testGetFileContents(): void { $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR; - $this->assertInternalType('string', File::fileGetContents($pathResources . 'images' . DIRECTORY_SEPARATOR . 'PHPPowerPointLogo.png')); + $this->assertIsString(File::fileGetContents($pathResources . 'images' . DIRECTORY_SEPARATOR . 'PHPPowerPointLogo.png')); $this->assertNull(File::fileGetContents($pathResources . 'images' . DIRECTORY_SEPARATOR . 'PHPPowerPointLogo_404.png')); - $this->assertInternalType('string', File::fileGetContents('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#[Content_Types].xml')); + $this->assertIsString(File::fileGetContents('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#[Content_Types].xml')); $this->assertNull(File::fileGetContents('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#404.xml')); $this->assertNull(File::fileGetContents('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . '404.pptx#404.xml')); } diff --git a/tests/Common/Tests/XMLReaderTest.php b/tests/Common/Tests/XMLReaderTest.php index b5a345c..02603d5 100644 --- a/tests/Common/Tests/XMLReaderTest.php +++ b/tests/Common/Tests/XMLReaderTest.php @@ -17,14 +17,17 @@ namespace PhpOffice\Common\Tests; +use Exception; +use InvalidArgumentException; use PhpOffice\Common\XMLReader; +use PHPUnit\Framework\TestCase; /** * Test class for XMLReader * * @coversDefaultClass \PhpOffice\Common\XMLReader */ -class XMLReaderTest extends \PHPUnit\Framework\TestCase +class XMLReaderTest extends TestCase { /** * Test reading XML from string @@ -58,11 +61,12 @@ public function testDomFromZip(): void /** * Test that read from non existing archive throws exception - * - * @expectedException \Exception */ public function testThrowsExceptionOnNonExistingArchive(): void { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Cannot find archive file.'); + $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR; $reader = new XMLReader(); @@ -86,7 +90,7 @@ public function testCountElements(): void public function testReturnNullOnNonExistingNode(): void { $reader = new XMLReader(); - $this->assertEmpty($reader->getElements('/element/children')); + $this->assertCount(0, $reader->getElements('/element/children')); $reader->getDomFromString('AAA'); $this->assertNull($reader->getElement('/element/children')); @@ -125,11 +129,12 @@ public function testShouldParseXmlWithCustomNamespace(): void /** * Test that xpath fails if custom namespace is not registered - * - * @expectedException \InvalidArgumentException */ public function testShouldThowExceptionIfTryingToRegisterNamespaceBeforeReadingDoc(): void { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Dom needs to be loaded before registering a namespace'); + $reader = new XMLReader(); $reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace'); } diff --git a/tests/Common/Tests/XMLWriterTest.php b/tests/Common/Tests/XMLWriterTest.php index c8ba235..167f59d 100644 --- a/tests/Common/Tests/XMLWriterTest.php +++ b/tests/Common/Tests/XMLWriterTest.php @@ -62,9 +62,15 @@ public function testWriteAttributeShouldWriteFloatValueLocaleIndependent(): void $xmlWriter->writeAttribute('name', $value); $xmlWriter->endElement(); + // https://www.php.net/manual/en/language.types.string.php#language.types.string.casting + // As of PHP 8.0.0, the decimal point character is always .. + // Prior to PHP 8.0.0, the decimal point character is defined in the script's locale (category LC_NUMERIC). setlocale(LC_NUMERIC, 'de_DE.UTF-8', 'de'); - - $this->assertSame('1,2', (string) $value); + if (PHP_VERSION_ID > 80000) { + $this->assertSame('1.2', (string) $value); + } else { + $this->assertSame('1,2', (string) $value); + } $this->assertSame('' . chr(10), $xmlWriter->getData()); } } diff --git a/tests/Common/Tests/_includes/XmlDocument.php b/tests/Common/Tests/_includes/XmlDocument.php index aa5a09e..ff4ff8c 100644 --- a/tests/Common/Tests/_includes/XmlDocument.php +++ b/tests/Common/Tests/_includes/XmlDocument.php @@ -155,9 +155,10 @@ public function getPath(): string * * @return string */ - public function getElementAttribute(string $path, string $attribute, string $file = 'word/document.xml'): string + public function getElementAttribute(string $path, string $attribute, string $file = 'word/document.xml'): string { $element = $this->getElement($path, $file); + return $element instanceof DOMElement ? $element->getAttribute($attribute) : ''; } @@ -173,6 +174,7 @@ public function getElementAttribute(string $path, string $attribute, string $fil public function attributeElementExists(string $path, string $attribute, string $file = 'word/document.xml'): bool { $element = $this->getElement($path, $file); + return $element instanceof DOMElement ? $element->hasAttribute($attribute) : false; } From 26a63a1e7a886c06806bcb2d76eadd291657b697 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Tue, 20 Jul 2021 13:48:47 +0200 Subject: [PATCH 13/19] Set minimal support to PHP 7.1 --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 613c402..b84cb46 100644 --- a/composer.json +++ b/composer.json @@ -15,11 +15,11 @@ } ], "require": { - "php": ">=5.3.0", + "php": ">=7.1", "pclzip/pclzip": "^2.8" }, "require-dev": { - "phpunit/phpunit": ">=7.0", + "phpunit/phpunit": ">=7", "phpmd/phpmd": "2.*", "phpstan/phpstan": "^0.12.88" }, From 378eae21fe312fb08605ffcff25adaa697d55693 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Tue, 20 Jul 2021 14:01:07 +0200 Subject: [PATCH 14/19] Fixed some errors --- .github/workflows/php.yml | 2 +- .github/workflows/phpmd.xml.dist | 37 -------------------------------- README.md | 7 +++--- 3 files changed, 4 insertions(+), 42 deletions(-) delete mode 100644 .github/workflows/phpmd.xml.dist diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 8c84763..69d01b3 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -1,4 +1,4 @@ -name: PHPPresentation +name: PHPOffice\Common on: [push, pull_request] jobs: php-cs-fixer: diff --git a/.github/workflows/phpmd.xml.dist b/.github/workflows/phpmd.xml.dist deleted file mode 100644 index fca998a..0000000 --- a/.github/workflows/phpmd.xml.dist +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/README.md b/README.md index a55ff1d..e22415b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ [![Latest Stable Version](https://poser.pugx.org/phpoffice/common/v/stable.png)](https://packagist.org/packages/phpoffice/common) -[![Build Status](https://travis-ci.org/PHPOffice/Common.svg?branch=master)](https://travis-ci.org/PHPOffice/Common) -[![Code Quality](https://scrutinizer-ci.com/g/PHPOffice/Common/badges/quality-score.png?s=b5997ce59ac2816b4514f3a38de9900f6d492c1d)](https://scrutinizer-ci.com/g/PHPOffice/Common/) -[![Code Coverage](https://scrutinizer-ci.com/g/PHPOffice/Common/badges/coverage.png?s=742a98745725c562955440edc8d2c39d7ff5ae25)](https://scrutinizer-ci.com/g/PHPOffice/Common/) +[![PHPOffice\Common](https://github.com/PHPOffice/Common/actions/workflows/php.yml/badge.svg)](https://github.com/PHPOffice/Common/actions/workflows/php.yml) +[![Coverage Status](https://coveralls.io/repos/github/PHPOffice/Common/badge.svg?branch=develop)](https://coveralls.io/github/PHPOffice/Common?branch=develop) [![Total Downloads](https://poser.pugx.org/phpoffice/common/downloads.png)](https://packagist.org/packages/phpoffice/common) [![License](https://poser.pugx.org/phpoffice/common/license.png)](https://packagist.org/packages/phpoffice/common) [![Join the chat at https://gitter.im/PHPOffice/Common](https://img.shields.io/badge/GITTER-join%20chat-green.svg)](https://gitter.im/PHPOffice/Common) @@ -21,7 +20,7 @@ Read more about PHPPowerPoint: PHPOffice Common requires the following: -- PHP 5.3+ +- PHP 7.1+ ## Contributing From 84f494768903e63031452fedbf41f7b54dcaf97c Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Wed, 21 Jul 2021 17:47:43 +0200 Subject: [PATCH 15/19] Improved Unit Tests --- src/Common/File.php | 11 ++- src/Common/Text.php | 4 +- src/Common/XMLWriter.php | 10 +-- tests/Common/Tests/FileTest.php | 7 ++ tests/Common/Tests/TextTest.php | 20 +++-- tests/Common/Tests/XMLWriterTest.php | 114 +++++++++++++++++++++++++++ 6 files changed, 148 insertions(+), 18 deletions(-) diff --git a/src/Common/File.php b/src/Common/File.php index 0ed21a7..8ab931d 100644 --- a/src/Common/File.php +++ b/src/Common/File.php @@ -17,9 +17,8 @@ namespace PhpOffice\Common; -/** - * Drawing - */ +use ZipArchive; + class File { /** @@ -29,7 +28,7 @@ class File * * @return bool */ - public static function fileExists($pFilename) + public static function fileExists(string $pFilename): bool { // Sick construction, but it seems that // file_exists returns strange values when @@ -39,7 +38,7 @@ public static function fileExists($pFilename) $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6); $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1); - $zip = new \ZipArchive(); + $zip = new ZipArchive(); if ($zip->open($zipFile) === true) { $returnValue = ($zip->getFromName($archiveFile) !== false); $zip->close(); @@ -71,7 +70,7 @@ public static function fileGetContents(string $pFilename): ?string $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6); $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1); - $zip = new \ZipArchive(); + $zip = new ZipArchive(); if ($zip->open($zipFile) === true) { $returnValue = $zip->getFromName($archiveFile); $zip->close(); diff --git a/src/Common/Text.php b/src/Common/Text.php index 206efb5..1410f79 100644 --- a/src/Common/Text.php +++ b/src/Common/Text.php @@ -140,9 +140,9 @@ public static function isUTF8(string $value = ''): bool * * @param string|null $value * - * @return string + * @return string|null */ - public static function toUTF8(?string $value = ''): string + public static function toUTF8(?string $value = ''): ?string { if (!is_null($value) && !self::isUTF8($value)) { $value = utf8_encode($value); diff --git a/src/Common/XMLWriter.php b/src/Common/XMLWriter.php index 1c4b619..da520db 100644 --- a/src/Common/XMLWriter.php +++ b/src/Common/XMLWriter.php @@ -138,14 +138,14 @@ public function writeElementBlock(string $element, $attributes, string $value = * * @param bool $condition * @param string $element - * @param string $attribute + * @param string|null $attribute * @param mixed $value * * @return void */ - public function writeElementIf($condition, $element, $attribute = null, $value = null) + public function writeElementIf(bool $condition, string $element, ?string $attribute = null, $value = null) { - if ($condition == true) { + if ($condition) { if (is_null($attribute)) { $this->writeElement($element, $value); } else { @@ -165,9 +165,9 @@ public function writeElementIf($condition, $element, $attribute = null, $value = * * @return void */ - public function writeAttributeIf($condition, $attribute, $value) + public function writeAttributeIf(bool $condition, string $attribute, $value) { - if ($condition == true) { + if ($condition) { $this->writeAttribute($attribute, $value); } } diff --git a/tests/Common/Tests/FileTest.php b/tests/Common/Tests/FileTest.php index 91fe57a..557644f 100644 --- a/tests/Common/Tests/FileTest.php +++ b/tests/Common/Tests/FileTest.php @@ -34,6 +34,13 @@ public function testFileExists(): void $this->assertTrue(File::fileExists('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#[Content_Types].xml')); $this->assertFalse(File::fileExists('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#404.xml')); $this->assertFalse(File::fileExists('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . '404.pptx#404.xml')); + + // Set a ZIP en ReadOnly Mode + $zipTest = tempnam(sys_get_temp_dir(), 'PhpOfficeCommon'); + copy($pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx', $zipTest); + chmod($zipTest, 333); + $this->assertFalse(File::fileExists('zip://' . $zipTest)); + unlink($zipTest); } public function testGetFileContents(): void diff --git a/tests/Common/Tests/TextTest.php b/tests/Common/Tests/TextTest.php index 3ba94d7..443163a 100644 --- a/tests/Common/Tests/TextTest.php +++ b/tests/Common/Tests/TextTest.php @@ -27,15 +27,21 @@ */ class TextTest extends TestCase { - public function testControlCharacters(): void + public function testControlCharactersPHP2OOXML(): void { $this->assertEquals('', Text::controlCharacterPHP2OOXML()); $this->assertEquals('aeiou', Text::controlCharacterPHP2OOXML('aeiou')); $this->assertEquals('àéîöù', Text::controlCharacterPHP2OOXML('àéîöù')); $value = rand(0, 8); - $this->assertEquals('_x' . sprintf('%04s', strtoupper(dechex($value))) . '_', Text::controlCharacterPHP2OOXML(chr($value))); + $this->assertEquals( + '_x' . sprintf('%04s', strtoupper(dechex($value))) . '_', + Text::controlCharacterPHP2OOXML(chr($value)) + ); + } + public function testControlCharactersOOXML2PHP(): void + { $this->assertEquals('', Text::controlCharacterOOXML2PHP('')); $this->assertEquals(chr(0x08), Text::controlCharacterOOXML2PHP('_x0008_')); } @@ -60,9 +66,6 @@ public function testChr(): void $this->assertEquals('', Text::chr(2097152)); } - /** - * Is UTF8 - */ public function testIsUTF8(): void { $this->assertTrue(Text::isUTF8('')); @@ -70,6 +73,13 @@ public function testIsUTF8(): void $this->assertFalse(Text::isUTF8(utf8_decode('éééé'))); } + public function testToUtf8(): void + { + $this->assertNull(Text::toUTF8(null)); + $this->assertEquals('eeee', Text::toUTF8('eeee')); + $this->assertEquals('éééé', Text::toUTF8('éééé')); + } + /** * Test unicode conversion */ diff --git a/tests/Common/Tests/XMLWriterTest.php b/tests/Common/Tests/XMLWriterTest.php index 167f59d..53a237c 100644 --- a/tests/Common/Tests/XMLWriterTest.php +++ b/tests/Common/Tests/XMLWriterTest.php @@ -43,6 +43,30 @@ public function testConstruct(): void $this->assertEquals('BBB' . chr(10), $object->getData()); } + public function testConstructCompatibility(): void + { + $object = new XMLWriter(XMLWriter::STORAGE_MEMORY, null, false); + $object->startElement('element'); + $object->startElement('sub'); + $object->text('CCC'); + $object->endElement(); + $object->endElement(); + $this->assertEquals( + '' . PHP_EOL . ' CCC' . PHP_EOL . '' . PHP_EOL, + $object->getData() + ); + $object = new XMLWriter(XMLWriter::STORAGE_MEMORY, null, true); + $object->startElement('element'); + $object->startElement('sub'); + $object->text('CCC'); + $object->endElement(); + $object->endElement(); + $this->assertEquals( + 'CCC', + $object->getData() + ); + } + public function testWriteAttribute(): void { $xmlWriter = new XMLWriter(); @@ -53,6 +77,23 @@ public function testWriteAttribute(): void $this->assertSame('' . chr(10), $xmlWriter->getData()); } + public function testWriteAttributeIf(): void + { + $xmlWriter = new XMLWriter(); + $xmlWriter->startElement('element'); + $xmlWriter->writeAttributeIf(true, 'name', 'value'); + $xmlWriter->endElement(); + + $this->assertSame('' . chr(10), $xmlWriter->getData()); + + $xmlWriter = new XMLWriter(); + $xmlWriter->startElement('element'); + $xmlWriter->writeAttributeIf(false, 'name', 'value'); + $xmlWriter->endElement(); + + $this->assertSame('' . chr(10), $xmlWriter->getData()); + } + public function testWriteAttributeShouldWriteFloatValueLocaleIndependent(): void { $value = 1.2; @@ -73,4 +114,77 @@ public function testWriteAttributeShouldWriteFloatValueLocaleIndependent(): void } $this->assertSame('' . chr(10), $xmlWriter->getData()); } + + public function testWriteElementBlock(): void + { + $xmlWriter = new XMLWriter(); + $xmlWriter->writeElementBlock('element', 'name'); + + $this->assertSame('' . chr(10), $xmlWriter->getData()); + + $xmlWriter = new XMLWriter(); + $xmlWriter->writeElementBlock('element', 'name', 'value'); + + $this->assertSame('' . chr(10), $xmlWriter->getData()); + + $xmlWriter = new XMLWriter(); + $xmlWriter->writeElementBlock('element', ['name' => 'value']); + + $this->assertSame('' . chr(10), $xmlWriter->getData()); + + $xmlWriter = new XMLWriter(); + $xmlWriter->writeElementBlock('element', ['name' => 'value'], 'value2'); + + $this->assertSame('' . chr(10), $xmlWriter->getData()); + } + + /** + * @dataProvider dataProviderWriteElementIf + */ + public function testWriteElementIf(bool $condition, ?string $attribute, ?string $value, string $expected): void + { + $xmlWriter = new XMLWriter(); + $xmlWriter->writeElementIf($condition, 'element', $attribute, $value); + + $this->assertSame($expected, $xmlWriter->getData()); + } + + /** + * @return array> + */ + public function dataProviderWriteElementIf(): array + { + return [ + [ + false, + null, + null, + '', + ], + [ + true, + null, + null, + '' . chr(10), + ], + [ + true, + 'attribute', + null, + '' . chr(10), + ], + [ + true, + null, + 'value', + 'value' . chr(10), + ], + [ + true, + 'attribute', + 'value', + '' . chr(10), + ], + ]; + } } From eca93ab7ae69fbcd1d57ecaf375b208d98b59d6a Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Wed, 21 Jul 2021 18:02:49 +0200 Subject: [PATCH 16/19] Fixed Drawing pixelsToPoints and twipsToPixels ratios Thanks @guilliamxavier --- src/Common/Drawing.php | 8 ++++---- tests/Common/Tests/DrawingTest.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Common/Drawing.php b/src/Common/Drawing.php index 4ec2dbf..1f248da 100644 --- a/src/Common/Drawing.php +++ b/src/Common/Drawing.php @@ -58,7 +58,7 @@ public static function emuToPixels(int $pValue = 0): float */ public static function pixelsToPoints(int $pValue = 0): float { - return $pValue * 0.67777777; + return $pValue * 0.75; } /** @@ -74,7 +74,7 @@ public static function pointsToCentimeters(int $pValue = 0): float return 0; } - return (($pValue * 1.333333333) / self::DPI_96) * 2.54; + return (($pValue / 0.75) / self::DPI_96) * 2.54; } /** @@ -90,7 +90,7 @@ public static function pointsToPixels(int $pValue = 0): float return 0; } - return $pValue * 1.333333333; + return $pValue / 0.75; } /** @@ -227,7 +227,7 @@ public static function twipsToPixels(int $pValue = 0): float return 0; } - return round($pValue / 15.873984); + return round($pValue / 15); } /** diff --git a/tests/Common/Tests/DrawingTest.php b/tests/Common/Tests/DrawingTest.php index 7f902de..a8fa1b8 100644 --- a/tests/Common/Tests/DrawingTest.php +++ b/tests/Common/Tests/DrawingTest.php @@ -61,9 +61,9 @@ public function testPixelsPoints(): void $value = rand(1, 100); $this->assertEquals(0, Drawing::pixelsToPoints()); - $this->assertEquals($value * 0.67777777, Drawing::pixelsToPoints($value)); + $this->assertEquals($value * 0.75, Drawing::pixelsToPoints($value)); $this->assertEquals(0, Drawing::pointsToPixels()); - $this->assertEquals($value * 1.333333333, Drawing::pointsToPixels($value)); + $this->assertEquals($value / 0.75, Drawing::pointsToPixels($value)); } public function testPointsCentimeters(): void @@ -71,7 +71,7 @@ public function testPointsCentimeters(): void $value = rand(1, 100); $this->assertEquals(0, Drawing::pointsToCentimeters()); - $this->assertEquals($value * 1.333333333 / Drawing::DPI_96 * 2.54, Drawing::pointsToCentimeters($value)); + $this->assertEquals($value / 0.75 / Drawing::DPI_96 * 2.54, Drawing::pointsToCentimeters($value)); } public function testTwips(): void @@ -94,7 +94,7 @@ public function testTwips(): void // Pixels $this->assertEquals(0, Drawing::twipsToPixels()); - $this->assertEquals(round($value / 15.873984), Drawing::twipsToPixels($value)); + $this->assertEquals(round($value / 15), Drawing::twipsToPixels($value)); } public function testHTML(): void From 7b4e636891058b249b4b0629a2484ce194a854b7 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Sun, 1 Aug 2021 22:00:33 +0200 Subject: [PATCH 17/19] Added conversion from centimeters to points --- src/Common/Drawing.php | 16 ++++++++++++++++ tests/Common/Tests/DrawingTest.php | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/src/Common/Drawing.php b/src/Common/Drawing.php index 1f248da..b4674de 100644 --- a/src/Common/Drawing.php +++ b/src/Common/Drawing.php @@ -77,6 +77,22 @@ public static function pointsToCentimeters(int $pValue = 0): float return (($pValue / 0.75) / self::DPI_96) * 2.54; } + /** + * Convert centimeters width to points + * + * @param float $pValue Value in centimeters + * + * @return float + */ + public static function centimetersToPoints(float $pValue = 0): float + { + if ($pValue == 0) { + return 0; + } + + return ($pValue / 2.54) * self::DPI_96 * 0.75; + } + /** * Convert points width to pixels * diff --git a/tests/Common/Tests/DrawingTest.php b/tests/Common/Tests/DrawingTest.php index a8fa1b8..a545c06 100644 --- a/tests/Common/Tests/DrawingTest.php +++ b/tests/Common/Tests/DrawingTest.php @@ -74,6 +74,13 @@ public function testPointsCentimeters(): void $this->assertEquals($value / 0.75 / Drawing::DPI_96 * 2.54, Drawing::pointsToCentimeters($value)); } + public function testCentimetersPoints(): void + { + $this->assertEquals(0, Drawing::centimetersToPoints()); + $this->assertEquals(28.346456692913385, Drawing::centimetersToPoints(1)); + $this->assertEquals(31.181102362204726, Drawing::centimetersToPoints(1.1)); + } + public function testTwips(): void { $value = rand(1, 100); From 1a1eb0a5cca0a6fec6e83443fc601fc2cb176663 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Mon, 2 Aug 2021 13:53:41 +0200 Subject: [PATCH 18/19] Fixed typing & Added conversion from points to EMU --- src/Common/Drawing.php | 38 +++++++++++++++++++++--------- tests/Common/Tests/DrawingTest.php | 10 +++++++- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/Common/Drawing.php b/src/Common/Drawing.php index b4674de..2c2ff4d 100644 --- a/src/Common/Drawing.php +++ b/src/Common/Drawing.php @@ -24,11 +24,11 @@ class Drawing /** * Convert pixels to EMU * - * @param int $pValue Value in pixels + * @param float $pValue Value in pixels * * @return float */ - public static function pixelsToEmu(int $pValue = 0): float + public static function pixelsToEmu(float $pValue = 0): float { return round($pValue * 9525); } @@ -38,15 +38,15 @@ public static function pixelsToEmu(int $pValue = 0): float * * @param int $pValue Value in EMU * - * @return float + * @return int */ - public static function emuToPixels(int $pValue = 0): float + public static function emuToPixels(int $pValue = 0): int { if ($pValue == 0) { return 0; } - return round($pValue / 9525); + return (int) round($pValue / 9525); } /** @@ -96,11 +96,11 @@ public static function centimetersToPoints(float $pValue = 0): float /** * Convert points width to pixels * - * @param int $pValue Value in points + * @param float $pValue Value in points * * @return float */ - public static function pointsToPixels(int $pValue = 0): float + public static function pointsToPixels(float $pValue = 0): float { if ($pValue == 0) { return 0; @@ -125,17 +125,17 @@ public static function pixelsToCentimeters(int $pValue = 0): float /** * Convert centimeters width to pixels * - * @param int $pValue Value in centimeters + * @param float $pValue Value in centimeters * - * @return float + * @return int */ - public static function centimetersToPixels(int $pValue = 0): float + public static function centimetersToPixels(float $pValue = 0): int { if ($pValue == 0) { return 0; } - return ($pValue / 2.54) * self::DPI_96; + return (int) round((($pValue / 2.54) * self::DPI_96)); } /** @@ -246,6 +246,22 @@ public static function twipsToPixels(int $pValue = 0): float return round($pValue / 15); } + /** + * Convert points to emu + * + * @param float $pValue + * + * @return int + */ + public static function pointsToEmu(float $pValue = 0): int + { + if ($pValue == 0) { + return 0; + } + + return (int) round(($pValue / 0.75) / 9525); + } + /** * Convert HTML hexadecimal to RGB * diff --git a/tests/Common/Tests/DrawingTest.php b/tests/Common/Tests/DrawingTest.php index a545c06..d8106c7 100644 --- a/tests/Common/Tests/DrawingTest.php +++ b/tests/Common/Tests/DrawingTest.php @@ -43,7 +43,7 @@ public function testPixelsCentimeters(): void $this->assertEquals(0, Drawing::pixelsToCentimeters()); $this->assertEquals($value / Drawing::DPI_96 * 2.54, Drawing::pixelsToCentimeters($value)); $this->assertEquals(0, Drawing::centimetersToPixels()); - $this->assertEquals($value / 2.54 * Drawing::DPI_96, Drawing::centimetersToPixels($value)); + $this->assertEquals(round($value / 2.54 * Drawing::DPI_96), Drawing::centimetersToPixels($value)); } public function testPixelsEMU(): void @@ -74,6 +74,14 @@ public function testPointsCentimeters(): void $this->assertEquals($value / 0.75 / Drawing::DPI_96 * 2.54, Drawing::pointsToCentimeters($value)); } + public function testPointsEmu(): void + { + $value = rand(1, 100); + + $this->assertEquals(0, Drawing::pointsToEmu()); + $this->assertEquals(round($value / 0.75 / 9525), Drawing::pointsToEmu($value)); + } + public function testCentimetersPoints(): void { $this->assertEquals(0, Drawing::centimetersToPoints()); From 29901fc89e0dc25e030c5359f1ad16b0ca2a8e83 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Tue, 3 Aug 2021 12:40:01 +0200 Subject: [PATCH 19/19] `pointsToCentimeters` accept float as parameter --- src/Common/Drawing.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Common/Drawing.php b/src/Common/Drawing.php index 2c2ff4d..2f71865 100644 --- a/src/Common/Drawing.php +++ b/src/Common/Drawing.php @@ -64,11 +64,11 @@ public static function pixelsToPoints(int $pValue = 0): float /** * Convert points width to centimeters * - * @param int $pValue Value in points + * @param float $pValue Value in points * * @return float */ - public static function pointsToCentimeters(int $pValue = 0): float + public static function pointsToCentimeters(float $pValue = 0): float { if ($pValue == 0) { return 0;