From 3524b554f95723292912bef6ef91608827ed8d89 Mon Sep 17 00:00:00 2001 From: mscherer Date: Thu, 22 Nov 2018 12:09:08 +0100 Subject: [PATCH] phpstan. --- .gitattributes | 8 ++++++++ .travis.yml | 19 +++++++++++++++---- README.md | 14 -------------- composer.json | 4 +++- src/Data/stubs.php | 6 +----- src/MediaEmbed.php | 13 +++++++++---- src/Object/MediaObject.php | 26 +++++++++----------------- tests/phpstan.neon | 1 + 8 files changed, 46 insertions(+), 45 deletions(-) create mode 100644 tests/phpstan.neon diff --git a/.gitattributes b/.gitattributes index 926a808..3317069 100644 --- a/.gitattributes +++ b/.gitattributes @@ -34,3 +34,11 @@ *.mo binary *.pdf binary *.phar binary + +# Remove files for archives generated using `git archive` +phpunit.xml.dist export-ignore +.travis.yml export-ignore +.gitignore export-ignore +.gitattributes export-ignore +.editorconfig export-ignore +tests/phpstan.neon diff --git a/.travis.yml b/.travis.yml index 46c20e8..7819c80 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,20 +7,31 @@ php: - 7.0 - 7.3 +env: + global: + - DEFAULT=1 + matrix: fast_finish: true include: + - php: 5.6 + env: PREFER_LOWEST=1 + - php: 7.2 env: PHPCS=1 DEFAULT=0 before_script: - - composer install --prefer-dist --no-interaction - - if [[ $PHPCS != 1 ]]; then composer require phpunit/phpunit:"^5.0|^6.0|^7.0"; fi + - if [[ $PREFER_LOWEST != 1 ]]; then composer install --prefer-source --no-interaction ; fi + - if [[ $PREFER_LOWEST == 1 ]]; then composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable ; fi + + - if [[ $DEFAULT == 1 ]]; then composer require phpunit/phpunit:"^5.0|^6.0|^7.0"; fi script: - - if [[ $PHPCS != 1 ]]; then vendor/bin/phpunit ; fi - - if [[ $PHPCS == 1 ]]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --ignore=tests/test_files/ src/ tests/ ; fi + - if [[ $DEFAULT == 1 ]]; then vendor/bin/phpunit ; fi + + - if [[ $CHECKS == 1 ]]; then composer require --dev phpstan/phpstan-shim:"^0.10" && composer phpstan; fi + - if [[ $CHECKS == 1 ]]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --ignore=tests/test_files/ src/ tests/ ; fi notifications: email: false diff --git a/README.md b/README.md index 59aeb2e..7204217 100644 --- a/README.md +++ b/README.md @@ -41,20 +41,6 @@ composer require dereuromark/media-embed ``` This will get the latest tagged version for you. -Or manually add the dependency to your `composer.json`: -```json -{ - "require": { - "dereuromark/media-embed": "0.*" - } -} -``` -and run: - -```bash -composer install -``` - ## API Overview ### Parsing diff --git a/composer.json b/composer.json index 87c8887..bc3c642 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ "jbroadway/urlify": "^1.0.0" }, "require-dev":{ - "fig-r/psr2r-sniffer": "dev-master" + "fig-r/psr2r-sniffer": "dev-master", + "phpstan/phpstan-shim": "^0.10.5" }, "autoload": { "psr-4": { @@ -31,6 +32,7 @@ "test": "php phpunit.phar", "test-coverage": "php phpunit.phar --log-junit tmp/coverage/unitreport.xml --coverage-html tmp/coverage --coverage-clover tmp/coverage/coverage.xml", "test-setup": "[ ! -f phpunit.phar ] && wget https://phar.phpunit.de/phpunit.phar || true", + "phpstan": "phpstan analyse -c tests/phpstan.neon -l 5 src/", "cs-check": "phpcs -v -p --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --extensions=php --ignore=tests/test_files/ src/ tests/", "cs-fix": "phpcbf -v --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --extensions=php --ignore=tests/test_files/ src/ tests/" } diff --git a/src/Data/stubs.php b/src/Data/stubs.php index 90f18af..5fd4632 100644 --- a/src/Data/stubs.php +++ b/src/Data/stubs.php @@ -304,7 +304,6 @@ [ 'name' => 'CollegeHumor', 'website' => 'http://www.collegehumour.com', - 'name' => 'CollegeHumor', 'url-match' => 'http://(?:www\.)?collegehumor\.com/video:([0-9]{1,12})', 'embed-src' => 'http://www.collegehumor.com/moogaloop/moogaloop.swf?clip_id=$2', 'embed-width' => '480', @@ -386,7 +385,6 @@ [ 'name' => 'Dropshots', 'website' => 'http://www.dropshots.com', - 'name' => 'dropshots.com', 'url-match' => '(http://media(?:[0-9]{0,2})\.dropshots\.com/photos(?:(?:/(?:[0-9]{1,10})){1,3})\.flv)', 'embed-src' => 'http://www.dropshots.com/dropshotsplayer.swf?url=$2', 'embed-width' => '480', @@ -520,7 +518,6 @@ [ 'name' => 'GameTube', 'website' => 'http://www.gametube.org', - 'name' => 'Gametube.org', 'url-match' => 'http://(?:www\.)?gametube\.org/(?:\#/video/|htmlVideo\.jsp\?id=|miniPlayer\.swf\?vidId=)([A-z0-9=_-]{28})', 'embed-src' => 'http://www.gametube.org/miniPlayer.swf?vidId=$2', 'embed-width' => '425', @@ -1224,7 +1221,6 @@ [ 'name' => 'Tu', 'website' => 'http://www.tu.tv', - 'name' => 'Tu.tv', 'url-match' => '(http://tu\.tv/tutvweb\.swf\?xtp=(?:[0-9]{1,10}))', 'embed-src' => '$2', 'embed-width' => '425', @@ -1510,7 +1506,7 @@ 'embed-height' => '320', 'flashvars' => 'config=http://www.mehr-schbass.de/videos/xml-config.php?id=$2-embed', 'id' => '$2', - 'name' => '$3', + //'name' => '$3', ], ]; diff --git a/src/MediaEmbed.php b/src/MediaEmbed.php index bb0074f..1eeed8a 100644 --- a/src/MediaEmbed.php +++ b/src/MediaEmbed.php @@ -142,7 +142,12 @@ protected function _matchUrl($url, array $regexRules) { protected function _parseLink($url, $regex) { $context = stream_context_create( ['http' => ['header' => 'Connection: close']]); - $source = preg_replace('/[^(\x20-\x7F)]*/', '', file_get_contents($url, 0, $context)); + $content = file_get_contents($url, false, $context); + if (!$content) { + return false; + } + + $source = preg_replace('/[^(\x20-\x7F)]*/', '', $content); if (preg_match('~' . $regex . '~imu', $source, $match)) { $this->_match = $match; @@ -172,7 +177,7 @@ public function setHosts(array $stubs, $reset = false) { /** * @param array $whitelist (alias/keys) - * @return array hostInfos or false on failure + * @return array Host info */ public function getHosts($whitelist = []) { if ($whitelist) { @@ -190,14 +195,14 @@ public function getHosts($whitelist = []) { /** * @param string $alias - * @return array hostInfos or false on failure + * @return array|null Host info or null on failure */ public function getHost($alias) { if (!$this->_hosts) { $this->_hosts = $this->getHosts(); } if (empty($this->_hosts[$alias])) { - return false; + return null; } return $this->_hosts[$alias]; } diff --git a/src/Object/MediaObject.php b/src/Object/MediaObject.php index 24f5b3b..b59a7c7 100644 --- a/src/Object/MediaObject.php +++ b/src/Object/MediaObject.php @@ -2,8 +2,6 @@ namespace MediaEmbed\Object; -use MediaEmbed\Object\ObjectInterface; - /** * A generic object - for now. * @@ -55,7 +53,7 @@ class MediaObject implements ObjectInterface { * @param array $config */ public function __construct(array $stub, array $config) { - $this->config = $config += $this->config; + $this->config = $config + $this->config; $stubDefaults = [ 'id' => '', @@ -184,7 +182,7 @@ public function website() { /** * Returns a png img * - * @return resource|null Resource or null if not available + * @return string|null Resource content or null if not available */ public function icon() { $url = $this->_stub['website']; @@ -201,7 +199,7 @@ public function icon() { $context = stream_context_create( ['http' => ['header' => 'Connection: close']]); // E.g. http://www.google.com/s2/favicons?domain=xyz.com - $file = file_get_contents($icon, 0, $context); + $file = file_get_contents($icon, false, $context); if ($file === false) { return null; } @@ -210,11 +208,11 @@ public function icon() { } /** - * @param string|null $location Absolute path with trailing slash + * @param string $location Absolute path with trailing slash * @param string|null $icon Icon data * @return string|null $filename */ - public function saveIcon($location = null, $icon = null) { + public function saveIcon($location, $icon = null) { if ($icon === null) { $icon = $this->icon(); } @@ -222,11 +220,7 @@ public function saveIcon($location = null, $icon = null) { return null; } if (!$location) { - $location = IMAGES . 'content' . DS . 'video_types'; - if (!is_dir($location)) { - mkdir($location, 0755, true); - } - $location .= DS; + return null; } $filename = $this->slug() . '.png'; $file = $location . $filename; @@ -274,7 +268,7 @@ public function setParam($param, $value = null) { * * @param mixed $param The name of the attribute to be set * or an array of multiple attribs to be set - * @param string|null $value (optional) the value to set the param to + * @param string|int|null $value (optional) the value to set the param to * if only one param is being set * @return $this */ @@ -351,7 +345,7 @@ protected function _adjustDimensions($type, $fromType, $fromLength) { * Return object params about the video metadata * * @param string|null $key - * @return array|string - object params + * @return array|string|null Object params */ public function getParams($key = null) { if (!empty($this->_stub['iframe-player']) && $this->config['prefer'] === 'iframe') { @@ -377,7 +371,7 @@ public function getParams($key = null) { * Return object attribute * * @param string|null $key - * @return array - object attribute + * @return mixed Object attribute */ public function getAttributes($key = null) { if (!empty($this->_stub['iframe-player']) && $this->config['prefer'] === 'iframe') { @@ -455,8 +449,6 @@ protected function _getObjectSrc($type = 'embed-src') { } /** - * VideoLib::getImageSrc() - * * @return string|null */ public function getImageSrc() { diff --git a/tests/phpstan.neon b/tests/phpstan.neon new file mode 100644 index 0000000..9631d44 --- /dev/null +++ b/tests/phpstan.neon @@ -0,0 +1 @@ +parameters: