diff --git a/composer.json b/composer.json index 212dd61a..39612674 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ } ], "require": { - "php": ">=5.3" + "php": "^8.1" }, "require-dev": { "symfony/var-dumper": "~3.0" diff --git a/composer.lock b/composer.lock index a4d67723..00aff9e0 100644 --- a/composer.lock +++ b/composer.lock @@ -1,29 +1,91 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "hash": "e46872088430e0a997af1ce84ce8e6a9", - "content-hash": "09ef41760d9b42e76657396c4b6e9a85", - "packages": [], + "content-hash": "eeb1bc4bfe86df8075ef7030d3a50845", + "packages": [ + { + "name": "phpstan/phpstan", + "version": "1.9.2", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "d6fdf01c53978b6429f1393ba4afeca39cc68afa" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/d6fdf01c53978b6429f1393ba4afeca39cc68afa", + "reference": "d6fdf01c53978b6429f1393ba4afeca39cc68afa", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "issues": "https://github.com/phpstan/phpstan/issues", + "source": "https://github.com/phpstan/phpstan/tree/1.9.2" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2022-11-10T09:56:11+00:00" + } + ], "packages-dev": [ { "name": "symfony/polyfill-mbstring", - "version": "v1.1.1", + "version": "v1.27.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "1289d16209491b584839022f29257ad859b8532d" + "reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/1289d16209491b584839022f29257ad859b8532d", - "reference": "1289d16209491b584839022f29257ad859b8532d", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/8ad114f6b39e2c98a8b0e3bd907732c207c2b534", + "reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534", "shasum": "" }, "require": { - "php": ">=5.3.3" + "php": ">=7.1" + }, + "provide": { + "ext-mbstring": "*" }, "suggest": { "ext-mbstring": "For best performance" @@ -31,16 +93,20 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.1-dev" + "dev-main": "1.27-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" } }, "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Mbstring\\": "" - }, "files": [ "bootstrap.php" - ] + ], + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + } }, "notification-url": "https://packagist.org/downloads/", "license": [ @@ -65,38 +131,56 @@ "portable", "shim" ], - "time": "2016-01-20 09:13:37" + "support": { + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.27.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-11-03T14:55:06+00:00" }, { "name": "symfony/var-dumper", - "version": "v3.0.3", + "version": "v3.4.47", "source": { "type": "git", "url": "https://github.com/symfony/var-dumper.git", - "reference": "9a6a883c48acb215d4825ce9de61dccf93d62074" + "reference": "0719f6cf4633a38b2c1585140998579ce23b4b7d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/var-dumper/zipball/9a6a883c48acb215d4825ce9de61dccf93d62074", - "reference": "9a6a883c48acb215d4825ce9de61dccf93d62074", + "url": "https://api.github.com/repos/symfony/var-dumper/zipball/0719f6cf4633a38b2c1585140998579ce23b4b7d", + "reference": "0719f6cf4633a38b2c1585140998579ce23b4b7d", "shasum": "" }, "require": { - "php": ">=5.5.9", + "php": "^5.5.9|>=7.0.8", "symfony/polyfill-mbstring": "~1.0" }, + "conflict": { + "phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0" + }, "require-dev": { - "twig/twig": "~1.20|~2.0" + "ext-iconv": "*", + "twig/twig": "~1.34|~2.4" }, "suggest": { + "ext-iconv": "To convert non-UTF-8 strings to UTF-8 (or symfony/polyfill-iconv in case ext-iconv cannot be used).", + "ext-intl": "To show region name in time zone dump", "ext-symfony_debug": "" }, "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.0-dev" - } - }, "autoload": { "files": [ "Resources/functions/dump.php" @@ -128,7 +212,24 @@ "debug", "dump" ], - "time": "2016-02-13 09:23:44" + "support": { + "source": "https://github.com/symfony/var-dumper/tree/v3.4.47" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2020-10-24T10:57:07+00:00" } ], "aliases": [], @@ -137,7 +238,8 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=5.3" + "php": "^8.1" }, - "platform-dev": [] + "platform-dev": [], + "plugin-api-version": "2.3.0" } diff --git a/src/Capture.php b/src/Capture.php index fa982245..0c09391b 100644 --- a/src/Capture.php +++ b/src/Capture.php @@ -24,174 +24,132 @@ class Capture { /** * URL to capture the screen of - * - * @var string */ - protected $url; + protected string $url; /** * dom element top position - * @var string */ - protected $top; + protected string $top; /** * dom element left position - * @var string */ - protected $left; + protected string $left; /** * Width of the page to render - * - * @var int */ - protected $width = 1024; + protected int $width = 1024; /** * Height of the page to render - * - * @var int */ - protected $height = 768; + protected int $height = 768; /** * Width of the page to clip - * - * @var int */ - protected $clipWidth; + protected int $clipWidth; /** * Height of the page to clip - * - * @var int */ - protected $clipHeight; + protected int $clipHeight; /** * Default body background color is white - * - * @var string */ - protected $backgroundColor = ''; + protected string $backgroundColor = ''; /** * Image Type, default is jpeg - * - * @var Type */ - protected $imageType; + protected Type $imageType; /** * User Agent String used on the page request - * - * @var string */ - protected $userAgentString = ''; + protected string $userAgentString = ''; - /** - * Sets the option to block analytics from being pinged - * - * @var boolean - */ - protected $blockAnalytics = false; + /** + * Sets the option to block analytics from being pinged + */ + protected bool $blockAnalytics = false; /** * Sets the timeout period - * - * @var int */ - protected $timeout = 0; + protected int $timeout = 0; - /** + /** * Sets the delay period - * - * @var int */ - protected $delay = 0; + protected int $delay = 0; /** * Bin directory, should contain the phantomjs file, otherwise it won't work - * - * @var string */ - public $binPath; + public string $binPath; /** * Template directory, directory in which will be the js templates files to execute - * - * @var string */ - public $templatePath; + public string $templatePath; /** * Jobs directory, directory for temporary files to be written and executed with phantomjs - * - * @var Jobs */ - public $jobs; + public Jobs $jobs; /** * Base directory to save the output files - * - * @var Output */ - public $output; + public Output $output; /** * Location where the file was written to - * - * @var string */ - protected $imageLocation; + protected string $imageLocation; /** * List of included JS scripts - * - * @var array */ - protected $includedJsScripts = array(); + protected array $includedJsScripts = []; /** * List of included JS snippets - * - * @var array */ - protected $includedJsSnippets = array(); + protected array $includedJsSnippets = []; /** * List of options which will be passed to phantomjs - * - * @var array */ - protected $options = array(); + protected array $options = []; /** - * Sets to keep the cookies between save(). - * - * @var bool - */ - protected $keepCookies; + * Sets to keep the cookies between save(). + */ + protected bool $keepCookies = false; /** - * CookieJar to put cookies saved. - * - * @var CookieJar - */ - public $cookieJar; + * CookieJar to put cookies saved. + */ + public CookieJar $cookieJar; /** * Capture constructor. */ - public function __construct($url = null) + public function __construct(?string $url = null) { if ($url) { $this->setUrl($url); } - $this->binPath = realpath(implode(DIRECTORY_SEPARATOR, array(dirname(__FILE__), '..', 'bin'))) . DIRECTORY_SEPARATOR; - $this->templatePath = realpath(implode(DIRECTORY_SEPARATOR, array(dirname(__FILE__), '..', 'templates'))) . DIRECTORY_SEPARATOR; + $this->binPath = realpath(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'bin'])) . DIRECTORY_SEPARATOR; + $this->templatePath = realpath( + implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'templates']) + ) . DIRECTORY_SEPARATOR; $this->jobs = new Jobs(); $this->output = new Output(); @@ -203,12 +161,11 @@ public function __construct($url = null) /** * Saves the screenshot to the requested location * - * @param string $imageLocation Image Location - * @param bool $deleteFileIfExists True to delete the file if it exists - * - * @return bool + * @param string $imageLocation Image Location + * @param bool $deleteFileIfExists True to delete the file if it exists + * @throws PhantomJsException */ - public function save($imageLocation, $deleteFileIfExists = true) + public function save(string $imageLocation, bool $deleteFileIfExists = true): bool { $this->imageLocation = $this->output->getLocation() . $imageLocation; @@ -216,12 +173,12 @@ public function save($imageLocation, $deleteFileIfExists = true) $this->imageLocation .= '.' . $this->getImageType()->getFormat(); } - $data = array( - 'url' => (string) $this->url, - 'width' => $this->width, - 'height' => $this->height, - 'imageLocation' => LocalPath::sanitize($this->imageLocation), - ); + $data = [ + 'url' => $this->url, + 'width' => $this->width, + 'height' => $this->height, + 'imageLocation' => LocalPath::sanitize($this->imageLocation) + ]; if ($this->clipWidth && $this->clipHeight) { $data['clipOptions']['width'] = $this->clipWidth; @@ -258,9 +215,9 @@ public function save($imageLocation, $deleteFileIfExists = true) $data['includedJsSnippets'] = $this->includedJsSnippets; } - if ($this->blockAnalytics) { - $data['blockAnalytics'] = $this->blockAnalytics; - } + if ($this->blockAnalytics) { + $data['blockAnalytics'] = $this->blockAnalytics; + } if ($deleteFileIfExists && file_exists($this->imageLocation) && is_writable($this->imageLocation)) { unlink($this->imageLocation); @@ -271,14 +228,14 @@ public function save($imageLocation, $deleteFileIfExists = true) $data['cookieJar'] = $this->cookieJar->getCookiesJSON(); } - $jobName = md5(json_encode($data)); + $jobName = md5(json_encode($data, JSON_THROW_ON_ERROR)); $jobPath = $this->jobs->getLocation() . $jobName . '.js'; // Saves the cookies in the same folder as the jobs. $cookiesPath = $this->jobs->getLocation() . $jobName . '.json'; // Put the path in array. The js will pick up this filepath and save the cookies in it. - $data['cookiesPath'] = LocalPath::sanitize($cookiesPath); - + $data['cookiesPath'] = LocalPath::sanitize($cookiesPath); + if (!is_file($jobPath)) { // Now we write the code to a js file $resultString = $this->getTemplateResult('screen-capture', $data); @@ -305,13 +262,9 @@ public function save($imageLocation, $deleteFileIfExists = true) } /** - * @param string $templateName - * @param array $args - * - * @return string * @throws TemplateNotFoundException */ - private function getTemplateResult($templateName, array $args) + private function getTemplateResult(string $templateName, array $args): string { $templatePath = $this->templatePath . DIRECTORY_SEPARATOR . $templateName . '.php'; if (!file_exists($templatePath)) { @@ -324,17 +277,14 @@ private function getTemplateResult($templateName, array $args) return ob_get_clean(); } - /** - * @return string - */ - private function getOptionsString() + private function getOptionsString(): string { if (empty($this->options)) { return ''; } $mappedOptions = array_map(function ($value, $key) { - if (substr($key, 0, 2) === '--') { + if (str_starts_with($key, '--')) { $key = substr($key, 2); } @@ -346,12 +296,11 @@ private function getOptionsString() /** * Sets the path to PhantomJS binary, example: "/usr/bin" - * - * @param string $path + * @throws \Exception */ - public function setBinPath($binPath) + public function setBinPath(string $binPath): void { - $binPath = rtrim($binPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; + $binPath = rtrim((string)$binPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; if (!file_exists($binPath . 'phantomjs') && !file_exists($binPath . 'phantomjs.exe')) { throw new \Exception("Bin directory should contain phantomjs or phantomjs.exe file!"); } @@ -360,16 +309,12 @@ public function setBinPath($binPath) /** * Sets the url to screenshot - * - * @param string $url URL - * * @throws \Exception If the url is not valid * - * @return Capture */ - public function setUrl($url) + public function setUrl(string $url): Capture { - $this->url = new Url($url); + $this->url = $url; return $this; } @@ -378,10 +323,8 @@ public function setUrl($url) * Sets the dom top position * * @param int $top dom top position - * - * @return Capture */ - public function setTop($top) + public function setTop(int $top): Capture { $this->top = $top; @@ -392,10 +335,8 @@ public function setTop($top) * Sets the page width * * @param int $left dom left position - * - * @return Capture */ - public function setLeft($left) + public function setLeft(int $left): Capture { $this->left = $left; @@ -406,10 +347,8 @@ public function setLeft($left) * Sets the page width * * @param int $width Page Width - * - * @return Capture */ - public function setWidth($width) + public function setWidth(int $width): Capture { $this->width = $width; @@ -420,10 +359,8 @@ public function setWidth($width) * Sets the page height * * @param int $height Page Height - * - * @return Capture */ - public function setHeight($height) + public function setHeight(int $height): Capture { $this->height = $height; @@ -434,10 +371,8 @@ public function setHeight($height) * Sets the width to clip * * @param int $clipWidth Page clip width - * - * @return Capture */ - public function setClipWidth($clipWidth) + public function setClipWidth(int $clipWidth): Capture { $this->clipWidth = $clipWidth; @@ -448,10 +383,8 @@ public function setClipWidth($clipWidth) * Sets the height to clip * * @param int $clipHeight Page clip height - * - * @return Capture */ - public function setClipHeight($clipHeight) + public function setClipHeight(int $clipHeight): Capture { $this->clipHeight = $clipHeight; @@ -462,10 +395,8 @@ public function setClipHeight($clipHeight) * Sets the page body background color * * @param string $backgroundColor Background Color - * - * @return Capture */ - public function setBackgroundColor($backgroundColor) + public function setBackgroundColor(string $backgroundColor): Capture { $this->backgroundColor = $backgroundColor; @@ -476,68 +407,53 @@ public function setBackgroundColor($backgroundColor) * Sets the image type * * @param string $type 'jpg', 'png', etc... - * - * @return Capture + * @throws \Exception */ - public function setImageType($type) + public function setImageType(string $type): Capture { $this->imageType = Types::getClass($type); return $this; } - /** - * Sets the block analytics type - * - * @param boolean - * - * @return Capture - */ - public function setBlockAnalytics($boolean) - { - $this->blockAnalytics = $boolean; - - return $this; - } - - /** - * Returns the block analytics instance - * - * @return Type - */ - public function getBlockAnalytics() - { - return $this->blockAnalytics; - } + /** + * Sets the block analytics type + */ + public function setBlockAnalytics(bool $boolean): Capture + { + $this->blockAnalytics = $boolean; + + return $this; + } + + /** + * Returns the block analytics instance + */ + public function getBlockAnalytics(): Type|bool + { + return $this->blockAnalytics; + } /** * Returns the image type instance - * - * @return Type */ - public function getImageType() + public function getImageType(): Type { return $this->imageType; } /** * Returns the location where the screenshot file was written - * - * @return string */ - public function getImageLocation() + public function getImageLocation(): string { return $this->imageLocation; } /** * Sets the User Agent String to be used on the page request - * - * @param string $userAgentString User Agent String - * - * @return Capture */ - public function setUserAgentString($userAgentString) + public function setUserAgentString(string $userAgentString): Capture { $this->userAgentString = $userAgentString; @@ -547,12 +463,9 @@ public function setUserAgentString($userAgentString) /** * Sets the timeout period * - * @param int $timeout Timeout period - * - * @return Capture * @throws InvalidArgumentException */ - public function setTimeout($timeout) + public function setTimeout(int $timeout): Capture { if (!is_numeric($timeout)) { throw new InvalidArgumentException('The timeout value must be a number.'); @@ -566,11 +479,9 @@ public function setTimeout($timeout) * Sets the delay period * * @param int $delay Delay period - * - * @return Capture * @throws InvalidArgumentException */ - public function setDelay($delay) + public function setDelay(int $delay): Capture { if (!is_numeric($delay)) { throw new InvalidArgumentException('The delay value must be a number.'); @@ -579,14 +490,13 @@ public function setDelay($delay) return $this; } + /** * Adds a JS script or snippet to the screen shot script * * @param string|URL $script Script to include - * - * @return Capture */ - public function includeJs($script) + public function includeJs($script): Capture { if ($script instanceof Url) { $this->includedJsScripts[] = $script; @@ -599,12 +509,8 @@ public function includeJs($script) /** * Sets the options which will be passed to phantomjs - * - * @param array $options - * - * @return $this */ - public function setOptions($options) + public function setOptions(array $options): Capture { $this->options = $options; @@ -613,12 +519,8 @@ public function setOptions($options) /** * Sets to keep the cookies between save(). - * - * @param bool $choice - * - * @return $this */ - public function keepCookiesBetweenSave($choice) + public function keepCookiesBetweenSave(bool $choice): Capture { $this->keepCookies = $choice; return $this; diff --git a/src/CookieJar.php b/src/CookieJar.php index cf881db0..4177ba7b 100644 --- a/src/CookieJar.php +++ b/src/CookieJar.php @@ -2,6 +2,8 @@ namespace Screen; +use Screen\Exceptions\FileNotFoundException; + /** * Class CookieJar * @@ -13,49 +15,48 @@ class CookieJar { /** - * Cookies in JSON format. - * - * @var string - */ - protected $cookies; + * Cookies in JSON format. + */ + protected string $cookies; /** * Load cookies from file * - * @param string $file Path to file + * @param string $file Path to file + * @throws FileNotFoundException */ - public function load($file){ + public function load(string $file) + { $realPath = realpath($file); if (!$realPath || !file_exists($realPath)) { throw new FileNotFoundException($realPath); } - $this->cookies = file_get_contents($realPath); + $this->cookies = file_get_contents($realPath); } /** * Gets Cookies on JSON format - * - * @return string */ - public function getCookiesJSON(){ + public function getCookiesJSON(): string + { return $this->cookies; } /** * Sets Cookies on JSON format - * - * @param string */ - public function setCookiesJSON($cookiesJSON){ + public function setCookiesJSON(string $cookiesJSON): void + { $this->cookies = $cookiesJSON; } /** * Clear cookies from cookieJar. */ - public function empty(){ + public function empty(): void + { $this->cookies = null; } } diff --git a/src/Exceptions/FileNotFoundException.php b/src/Exceptions/FileNotFoundException.php index 8b3d801c..817fc0cd 100644 --- a/src/Exceptions/FileNotFoundException.php +++ b/src/Exceptions/FileNotFoundException.php @@ -2,9 +2,11 @@ namespace Screen\Exceptions; +use Exception; + class FileNotFoundException extends ScreenException { - public function __construct($path, $code = 0, Exception $previous = null) + public function __construct(string $path, int $code = 0, Exception $previous = null) { $message = sprintf("The file was not found at '%s'.", $path); diff --git a/src/Exceptions/InvalidArgumentException.php b/src/Exceptions/InvalidArgumentException.php index 4b64ec7e..4d91d673 100644 --- a/src/Exceptions/InvalidArgumentException.php +++ b/src/Exceptions/InvalidArgumentException.php @@ -2,9 +2,11 @@ namespace Screen\Exceptions; +use Exception; + class InvalidArgumentException extends ScreenException { - public function __construct($message, $code = 0, Exception $previous = null) + public function __construct(string $message, int $code = 0, Exception $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Exceptions/InvalidUrlException.php b/src/Exceptions/InvalidUrlException.php index 920c7527..3455ddce 100644 --- a/src/Exceptions/InvalidUrlException.php +++ b/src/Exceptions/InvalidUrlException.php @@ -2,9 +2,11 @@ namespace Screen\Exceptions; +use Exception; + class InvalidUrlException extends ScreenException { - public function __construct($url, $code = 0, Exception $previous = null) + public function __construct(string $url, int $code = 0, Exception $previous = null) { $message = sprintf("The url '%s' is not valid.", $url); diff --git a/src/Exceptions/PhantomJsException.php b/src/Exceptions/PhantomJsException.php index a23a6ac7..ccac1826 100644 --- a/src/Exceptions/PhantomJsException.php +++ b/src/Exceptions/PhantomJsException.php @@ -4,7 +4,7 @@ class PhantomJsException extends \Exception { - public function __construct($message, $code = 0, \Exception $previous = null) + public function __construct($message, int $code = 0, \Exception $previous = null) { $message = is_array($message) ? implode(PHP_EOL, $message) : $message; diff --git a/src/Exceptions/TemplateNotFoundException.php b/src/Exceptions/TemplateNotFoundException.php index 50a06463..75ba027d 100644 --- a/src/Exceptions/TemplateNotFoundException.php +++ b/src/Exceptions/TemplateNotFoundException.php @@ -2,9 +2,11 @@ namespace Screen\Exceptions; +use Exception; + class TemplateNotFoundException extends ScreenException { - public function __construct($templateName, $code = 0, Exception $previous = null) + public function __construct(string $templateName, int $code = 0, Exception $previous = null) { $message = sprintf("The template '%s' does not exist!", $templateName); diff --git a/src/Image/Types.php b/src/Image/Types.php index 7763d95e..83bd8fee 100644 --- a/src/Image/Types.php +++ b/src/Image/Types.php @@ -9,43 +9,29 @@ class Types { - protected static $typesMap = array( - Jpg::FORMAT => 'Screen\Image\Types\Jpg', - Png::FORMAT => 'Screen\Image\Types\Png', - Pdf::FORMAT => 'Screen\Image\Types\Pdf' - ); + protected static array $typesMap = [Jpg::FORMAT => \Screen\Image\Types\Jpg::class, Png::FORMAT => \Screen\Image\Types\Png::class, Pdf::FORMAT => \Screen\Image\Types\Pdf::class]; /** * Returns all the available image types - * - * @return array */ - public static function available() + public static function available(): array { return array_keys(static::$typesMap); } /** * Check if an image type is available - * - * @param $type - * - * @return bool */ - public static function isAvailable($type) + public static function isAvailable(string $type): bool { return in_array(strtolower($type), static::available()); } /** * Returns an instance of the requested image type - * - * @param string $type Image type - * - * @return Type * @throws \Exception */ - public static function getClass($type) + public static function getClass(string $type): Type { if (!static::isAvailable($type)) { throw new \Exception( diff --git a/src/Image/Types/Jpg.php b/src/Image/Types/Jpg.php index 86d8d48f..19e610df 100644 --- a/src/Image/Types/Jpg.php +++ b/src/Image/Types/Jpg.php @@ -4,7 +4,7 @@ class Jpg extends Type { - const FORMAT = 'jpg'; + final public const FORMAT = 'jpg'; - const MIME_TYPE = 'image/jpeg'; + final public const MIME_TYPE = 'image/jpeg'; } diff --git a/src/Image/Types/Pdf.php b/src/Image/Types/Pdf.php index 172bb28c..b0317dd2 100644 --- a/src/Image/Types/Pdf.php +++ b/src/Image/Types/Pdf.php @@ -4,7 +4,7 @@ class Pdf extends Type { - const FORMAT = 'pdf'; + final public const FORMAT = 'pdf'; - const MIME_TYPE = 'application/pdf'; + final public const MIME_TYPE = 'application/pdf'; } \ No newline at end of file diff --git a/src/Image/Types/Png.php b/src/Image/Types/Png.php index 9cd17823..52b776ec 100644 --- a/src/Image/Types/Png.php +++ b/src/Image/Types/Png.php @@ -4,7 +4,7 @@ class Png extends Type { - const FORMAT = 'png'; + final public const FORMAT = 'png'; - const MIME_TYPE = 'image/png'; + final public const MIME_TYPE = 'image/png'; } diff --git a/src/Image/Types/Type.php b/src/Image/Types/Type.php index 0d0d9646..8923e73d 100644 --- a/src/Image/Types/Type.php +++ b/src/Image/Types/Type.php @@ -4,26 +4,22 @@ abstract class Type { - const FORMAT = ''; + public const FORMAT = ''; - const MIME_TYPE = ''; + public const MIME_TYPE = ''; /** * Gets the image format - * - * @return string */ - public function getFormat() + public function getFormat(): string { return static::FORMAT; } /** * Gets the MIME type of resulted image - * - * @return string */ - public function getMimeType() + public function getMimeType(): string { return static::MIME_TYPE; } diff --git a/src/Injection/LocalPath.php b/src/Injection/LocalPath.php index 43fe32c5..910711ac 100644 --- a/src/Injection/LocalPath.php +++ b/src/Injection/LocalPath.php @@ -3,6 +3,7 @@ namespace Screen\Injection; use Screen\Exceptions\FileNotFoundException; +use Screen\Exceptions\InvalidUrlException; /** * Class LocalPath @@ -15,11 +16,10 @@ class LocalPath extends Url /** * LocalPath constructor. * - * @param string $url Local file path - * * @throws FileNotFoundException + * @throws InvalidUrlException */ - public function __construct($url) + public function __construct(string $url) { $filePath = realpath($url); @@ -28,16 +28,13 @@ public function __construct($url) } $this->src = self::sanitize($filePath); + parent::__construct($url); } /** * Sanitizes a path string - * - * @param string $path File Path - * - * @return string */ - public static function sanitize($path) + public static function sanitize(string $path): string { // If used on windows the \ char needs to be handled to be used on a string return str_replace("\\", "\\\\", $path); diff --git a/src/Injection/Url.php b/src/Injection/Url.php index 74355cd6..709f9bc3 100644 --- a/src/Injection/Url.php +++ b/src/Injection/Url.php @@ -8,12 +8,13 @@ class Url { /** * URL source - * - * @var string */ - protected $src; + protected string $src; - public function __construct($url) + /** + * @throws InvalidUrlException + */ + public function __construct(string $url) { // Prepend http:// if the url doesn't contain it if (!stristr($url, 'http://') && !stristr($url, 'https://')) { @@ -24,21 +25,16 @@ public function __construct($url) throw new InvalidUrlException($url); } - $url = str_replace(array(';', '"', 'src = $this->expandShortUrl($url); } - public function __toString() - { - return $this->src; - } - - public function expandShortUrl($url) + public function expandShortUrl(string $url): string { $headers = get_headers($url, 1) ; - if (array_key_exists('location',$headers)) { + if (array_key_exists('location', $headers)) { if ($headers['location'] != '') { return $headers['location']; } diff --git a/src/Location/Jobs.php b/src/Location/Jobs.php index 9df77bb8..6a2dab3d 100644 --- a/src/Location/Jobs.php +++ b/src/Location/Jobs.php @@ -17,7 +17,7 @@ class Jobs extends Location */ public function __construct() { - $defaultLocationPath = implode(DIRECTORY_SEPARATOR, array(dirname(__FILE__), '..', '..', 'jobs')); + $defaultLocationPath = implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', 'jobs']); if (!is_dir($defaultLocationPath)) { mkdir($defaultLocationPath, 0755); } @@ -27,7 +27,7 @@ public function __construct() /** * Deletes all the job files */ - public function clean() + public function clean(): void { $jobFiles = glob($this->getLocation() . '*.js'); foreach ($jobFiles as $file) { diff --git a/src/Location/Location.php b/src/Location/Location.php index 6cf4f2de..d1fd866f 100644 --- a/src/Location/Location.php +++ b/src/Location/Location.php @@ -13,22 +13,16 @@ abstract class Location { /** * Directory Path - * - * @var string */ - protected $location = ''; + protected string $location = ''; /** * Sets the files location * - * @param string $path Path to store the files - * * @throws \Exception If the location does not exist - * - * @return void */ - public function setLocation($locationPath) + public function setLocation(string $locationPath): void { $locationPath = realpath($locationPath); if (!$locationPath || !is_dir($locationPath)) { @@ -40,10 +34,8 @@ public function setLocation($locationPath) /** * Returns the location path - * - * @return string */ - public function getLocation() + public function getLocation(): string { return $this->location; }