From 42368a45ad69b6426021fff958013ff4dde92e62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Wed, 23 Nov 2022 13:28:55 +0100 Subject: [PATCH 1/4] style: remove trailing whitespace --- .circleci/config.yml | 12 +++--- LICENSE | 20 +++++----- README.md | 2 +- src/Imgix/UrlBuilder.php | 20 +++++----- src/Imgix/Validator.php | 6 +-- src/autoload.php | 1 - tests/Imgix/Tests/PathEncodingTest.php | 2 +- tests/Imgix/Tests/ReadMeTest.php | 8 ++-- tests/Imgix/Tests/UrlBuilderTest.php | 52 +++++++++++++------------- tests/Imgix/Tests/ValidatorTest.php | 2 +- 10 files changed, 62 insertions(+), 63 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 43f9dd6..727c419 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,12 +1,12 @@ version: 2.1 # Use 2.1 to enable using orbs and other features. - -orbs: + +orbs: php: circleci/php@1.1.0 jobs: test: parameters: - version: + version: default: "8.0" description: PHP version to install type: string @@ -27,15 +27,15 @@ jobs: equal: [ "8.1", <> ] steps: - run: composer update - + # install deps - run: composer install # run tests - - run: + - run: name: PHPUnit Tests command: vendor/bin/phpunit --bootstrap src/autoload.php tests - + workflows: version: 2 test: diff --git a/LICENSE b/LICENSE index 13b5738..972fc04 100644 --- a/LICENSE +++ b/LICENSE @@ -8,17 +8,17 @@ modification, are permitted provided that the following conditions are met: this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation + this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE -LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md index f2fe754..7e5c00f 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ $builder = new UrlBuilder("demos.imgix.net", true, "my-key", false); echo $builder->createSrcSet("image.png"); ``` -The above will produce the following srcset attribute value which can then be served to the client: +The above will produce the following srcset attribute value which can then be served to the client: ``` html https://demos.imgix.net/image.png?w=100&s=e415797545a77a9d2842dedcfe539c9a 100w, diff --git a/src/Imgix/UrlBuilder.php b/src/Imgix/UrlBuilder.php index bf0c6d0..c6cad77 100644 --- a/src/Imgix/UrlBuilder.php +++ b/src/Imgix/UrlBuilder.php @@ -37,7 +37,7 @@ public function __construct($domain, $useHttps = true, $signKey = "", $includeLi } $this->domain = $domain; - $this->validateDomain($this->domain); + $this->validateDomain($this->domain); $this->useHttps = $useHttps; $this->signKey = $signKey; @@ -49,9 +49,9 @@ private function validateDomain($domain) { $DOMAIN_PATTERN = "/^(?:[a-z\d\-_]{1,62}\.){0,125}(?:[a-z\d](?:\-(?=\-*[a-z\d])|[a-z]|\d){0,62}\.)[a-z\d]{1,63}$/"; if(!preg_match($DOMAIN_PATTERN, $domain)) { - throw new \InvalidArgumentException('Domain must be passed in as fully-qualified ' . + throw new \InvalidArgumentException('Domain must be passed in as fully-qualified ' . 'domain name and should not include a protocol or any path element, i.e. ' . - '"example.imgix.net".'); + '"example.imgix.net".'); } } @@ -87,7 +87,7 @@ public function createSrcSet($path, $params=array(), $options=array()) { Validator::validateWidths($widthsArray); return $this->createSrcSetPairs($path, $params=$params, $widthsArray); } - + $_start = isset($options['start']) ? $options['start'] : self::MIN_WIDTH; $_stop = isset($options['stop']) ? $options['stop'] : self::MAX_WIDTH; $_tol = isset($options['tol']) ? $options['tol'] : self::SRCSET_WIDTH_TOLERANCE; @@ -96,8 +96,8 @@ public function createSrcSet($path, $params=array(), $options=array()) { if ($this->isDpr($params)) { return $this->createDPRSrcSet( - $path=$path, - $params=$params, + $path=$path, + $params=$params, $disableVariableQuality=$_disableVariableQuality); } else { @@ -108,17 +108,17 @@ public function createSrcSet($path, $params=array(), $options=array()) { /** * Generate a list of target widths. - * + * * This function generates an array of target widths used to * width-describe image candidate strings (URLs) within a * srcset attribute. - * + * * This function returns an array of integer values that denote * image target widths. This array begins with `$start` * and ends with `$stop`. The `$tol` or tolerance value dictates * the amount of tolerable width variation between each width * in the range of values that lie between `$start` and `$stop`. - * + * * @param int $start Starting minimum width value. * @param int $stop Stopping maximum width value. * @param int $tol Tolerable amount of width variation. @@ -147,7 +147,7 @@ public function targetWidths( if (end($resolutions) < $stop) { array_push($resolutions, (int) $stop); } - + return $resolutions; } diff --git a/src/Imgix/Validator.php b/src/Imgix/Validator.php index e610408..3deb43a 100644 --- a/src/Imgix/Validator.php +++ b/src/Imgix/Validator.php @@ -13,7 +13,7 @@ public static function validateMinWidth($start) { public static function validateMaxWidth($end) { if ($end < 0) { - throw new \InvalidArgumentException("`stop` width value must be greater than zero"); + throw new \InvalidArgumentException("`stop` width value must be greater than zero"); } } @@ -25,7 +25,7 @@ public static function validateRange($start, $stop) { // Ensure that the range is valid, ie. `begin <= end`. if ($start > $stop) { - throw new \InvalidArgumentException("`start` width value must be less than `stop` width value"); + throw new \InvalidArgumentException("`start` width value must be less than `stop` width value"); } } @@ -56,4 +56,4 @@ public static function validateWidths($widths) { } } } -} \ No newline at end of file +} diff --git a/src/autoload.php b/src/autoload.php index 28fe10d..b71f861 100644 --- a/src/autoload.php +++ b/src/autoload.php @@ -23,4 +23,3 @@ return false; }); - diff --git a/tests/Imgix/Tests/PathEncodingTest.php b/tests/Imgix/Tests/PathEncodingTest.php index e20f3ee..ed43932 100644 --- a/tests/Imgix/Tests/PathEncodingTest.php +++ b/tests/Imgix/Tests/PathEncodingTest.php @@ -30,4 +30,4 @@ public function testUnicodeEncoding() { $url); } - } \ No newline at end of file + } diff --git a/tests/Imgix/Tests/ReadMeTest.php b/tests/Imgix/Tests/ReadMeTest.php index d0ed4fb..87dff8a 100644 --- a/tests/Imgix/Tests/ReadMeTest.php +++ b/tests/Imgix/Tests/ReadMeTest.php @@ -7,7 +7,7 @@ class ReadMeTest extends \PHPUnit\Framework\TestCase { public function testFixedWithImages() { $builder = new UrlBuilder("demos.imgix.net", true, "my-key", false); $actual = $builder->createSrcSet("image.png", array("h"=>800, "ar"=>"3:2", "fit"=>"crop")); - $expected = + $expected = "https://demos.imgix.net/image.png?ar=3%3A2&dpr=1&fit=crop&h=800&q=75&s=b6b4a327a9e5a9ce5c9251b736c98633 1x, https://demos.imgix.net/image.png?ar=3%3A2&dpr=2&fit=crop&h=800&q=50&s=4f96c2dffa682c081ba9b994c49222cc 2x, https://demos.imgix.net/image.png?ar=3%3A2&dpr=3&fit=crop&h=800&q=35&s=7b2a069e769cfeaf9e6dbb4679aea2bc 3x, @@ -51,7 +51,7 @@ public function testFluidWidthRanges() { $builder = new UrlBuilder("demo.imgix.net", true, "", false); $opts = array('start' => 500, 'stop' => 2000); $actual = $builder->createSrcSet($path="image.jpg", $params=array(), $options=$opts); - $expected = + $expected = 'https://demo.imgix.net/image.jpg?w=500 500w, https://demo.imgix.net/image.jpg?w=580 580w, https://demo.imgix.net/image.jpg?w=673 673w, @@ -71,7 +71,7 @@ public function testFluidWidthRangesTolerance() { $builder = new UrlBuilder("demo.imgix.net", true, "", false); $opts = array('start' => 100, 'stop' => 384, 'tol' => 0.20); $actual = $builder->createSrcSet($path="image.jpg", $params=array(), $options=$opts); - $expected = + $expected = 'https://demo.imgix.net/image.jpg?w=100 100w, https://demo.imgix.net/image.jpg?w=140 140w, https://demo.imgix.net/image.jpg?w=196 196w, @@ -80,4 +80,4 @@ public function testFluidWidthRangesTolerance() { $this->assertEquals($expected, $actual); } -} \ No newline at end of file +} diff --git a/tests/Imgix/Tests/UrlBuilderTest.php b/tests/Imgix/Tests/UrlBuilderTest.php index 51c1698..989a96d 100644 --- a/tests/Imgix/Tests/UrlBuilderTest.php +++ b/tests/Imgix/Tests/UrlBuilderTest.php @@ -36,7 +36,7 @@ public function testExamplePlainUsesHttpsByDefault() { // Construct a url in accordance with the other tests. $params = array("w" => 100, "h" => 100); // Create the url with the specified `$path` and `$params`. - $url = $builder->createURL("bridge.png", $params); + $url = $builder->createURL("bridge.png", $params); $this->assertEquals("https://demos.imgix.net/bridge.png?h=100&w=100", $url); } @@ -135,7 +135,7 @@ public function testNestedParameters() { public function test_invalid_domain_append_slash() { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Domain must be passed in as fully-qualified ' . + $this->expectExceptionMessage('Domain must be passed in as fully-qualified ' . 'domain name and should not include a protocol or any path element, i.e. ' . '"example.imgix.net".'); @@ -144,25 +144,25 @@ public function test_invalid_domain_append_slash() { public function test_invalid_domain_prepend_scheme() { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Domain must be passed in as fully-qualified ' . + $this->expectExceptionMessage('Domain must be passed in as fully-qualified ' . 'domain name and should not include a protocol or any path element, i.e. ' . '"example.imgix.net".'); - + $builder = new UrlBuilder("https://demos.imgix.net", true, "", false); } - + public function test_invalid_domain_append_dash() { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Domain must be passed in as fully-qualified ' . + $this->expectExceptionMessage('Domain must be passed in as fully-qualified ' . 'domain name and should not include a protocol or any path element, i.e. ' . '"example.imgix.net".'); - + $builder = new UrlBuilder("demos.imgix.net-", true, "", false); } private function srcsetBuilder($params=array()) { $builder = new UrlBuilder("demos.imgix.net", true, "my-key", false); - return $builder->createSrcSet("bridge.png", $params); + return $builder->createSrcSet("bridge.png", $params); } // parse the width as an int, eg "100w" => 100 @@ -196,11 +196,11 @@ public function testSrcsetCustomTargetWidths328to4088() { public function testTargetWidthsMaxTolerance() { $builder = new UrlBuilder("demos.imgix.net", true, "my-key", false); - $expected = array(0 => 100, 1 => 8192); + $expected = array(0 => 100, 1 => 8192); $actual = $builder->targetWidths($start=100, $stop=8192, $tol=10000000); $this->assertEquals($expected, $actual); } - + public function testNoParametersGeneratesSrcsetPairs() { $srcset = $this->srcsetBuilder(); $expectedNumberOfPairs = 31; @@ -217,8 +217,8 @@ public function testCustomSrcsetPairs() { $builder = new UrlBuilder("demos.imgix.net", true, false); $actual = $builder->createSrcSet( - $path="image.jpg", - $params=array(), + $path="image.jpg", + $params=array(), $options=array('start' => 720, 'stop' => 720)); $expected = 'https://demos.imgix.net/image.jpg?ixlib=php-3.3.1&w=720 720w'; @@ -254,10 +254,10 @@ public function testSrcsetPairValues() { $resolutions = self::TARGET_WIDTHS; $srclist = explode(",", $srcset); $matches = array(); - + foreach ($srclist as $src) { $width = explode(" ", $src)[1]; - + // extract width int values preg_match("/\d+/", $width, $matches); $this->assertEquals($resolutions[$index], $matches[0]); @@ -272,12 +272,12 @@ public function testGivenWidthSrcsetIsDPR() { foreach ($srclist as $src) { list($generatedURL, $generatedRatio) = explode(" ", $src); - + $dprStr = $devicePixelRatio . "x"; $this->assertEquals($dprStr, $generatedRatio); - + $this->assertMatchesRegularExpression("/dpr=".$devicePixelRatio."/", $generatedURL); - + $devicePixelRatio += 1; } } @@ -455,7 +455,7 @@ public function testGivenHeightSrcsetSignsUrls() { $this->assertEquals($expectSignature, $generatedSignature); } } - + public function testGivenWidthAndHeightSrcsetIsDPR() { $srcset = $this->srcsetBuilder(array("w"=>300, "h"=>"400")); $devicePixelRatio = 1; @@ -463,12 +463,12 @@ public function testGivenWidthAndHeightSrcsetIsDPR() { foreach ($srclist as $src) { list($generatedURL, $generatedRatio) = explode(" ", $src); - + $dprStr = $devicePixelRatio . "x"; $this->assertEquals($dprStr, $generatedRatio); - + $this->assertMatchesRegularExpression("/dpr=".$devicePixelRatio."/", $generatedURL); - + $devicePixelRatio += 1; } } @@ -480,7 +480,7 @@ public function testGivenWidthAndHeightSignsURLs() { foreach ($srclist as $src) { $url = explode(" ", $src)[0]; $this->assertMatchesRegularExpression("/s=/", $url); - + // parse out query params $params = substr($url, strrpos($url, "?")); $params = substr($params, 0, strrpos($params, "s=")-1); @@ -564,12 +564,12 @@ public function testGivenAspectRatioAndHeightSrcsetIsDPR() { foreach ($srclist as $src) { list($generatedURL, $generatedRatio) = explode(" ", $src); - + $dprStr = $devicePixelRatio . "x"; $this->assertEquals($dprStr, $generatedRatio); - + $this->assertMatchesRegularExpression("/dpr=".$devicePixelRatio."/", $generatedURL); - + $devicePixelRatio += 1; } } @@ -581,7 +581,7 @@ public function testGivenAspectRatioAndHeightSignsURLs() { foreach ($srclist as $src) { $url = explode(" ", $src)[0]; $this->assertMatchesRegularExpression("/s=/", $url); - + // parse out query params $params = substr($url, strrpos($url, "?")); $params = substr($params, 0, strrpos($params, "s=")-1); diff --git a/tests/Imgix/Tests/ValidatorTest.php b/tests/Imgix/Tests/ValidatorTest.php index fd61b9a..a04d00c 100644 --- a/tests/Imgix/Tests/ValidatorTest.php +++ b/tests/Imgix/Tests/ValidatorTest.php @@ -59,4 +59,4 @@ public function testValidateWidthsEmptyArray() { $this->expectException(InvalidArgumentException::class); Validator::validateWidths([]); } -} \ No newline at end of file +} From d4a755fd70647832feafa6447d0df0434ada359d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Wed, 23 Nov 2022 13:58:52 +0100 Subject: [PATCH 2/4] style: remove unused code --- src/autoload.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autoload.php b/src/autoload.php index b71f861..50ca879 100644 --- a/src/autoload.php +++ b/src/autoload.php @@ -14,7 +14,7 @@ } $fileName = __DIR__ . DIRECTORY_SEPARATOR . $fileName . $className . '.php'; - //$fileName = __DIR__ . DIRECTORY_SEPARATOR . $fileName . $namespace . '.php'; + if (is_file($fileName)) { require $fileName; From 4f68d65cbb47280370c227312a5c16a81ea1c6cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Wed, 23 Nov 2022 14:00:18 +0100 Subject: [PATCH 3/4] refactor: remove unnecessary class import not needed because both classes are defined in the same namespace --- src/Imgix/UrlBuilder.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Imgix/UrlBuilder.php b/src/Imgix/UrlBuilder.php index c6cad77..4a0035d 100644 --- a/src/Imgix/UrlBuilder.php +++ b/src/Imgix/UrlBuilder.php @@ -2,8 +2,6 @@ namespace Imgix; -use Imgix\Validator; - class UrlBuilder { private $currentVersion = "3.3.1"; From cc98253e7f4e5c6209dbdfa7560d94ce88961788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Lundgren?= Date: Wed, 23 Nov 2022 14:01:57 +0100 Subject: [PATCH 4/4] refactor: remove unused private class property defined in the constructor, but never used in the code --- src/Imgix/UrlBuilder.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Imgix/UrlBuilder.php b/src/Imgix/UrlBuilder.php index 4a0035d..abe47fa 100644 --- a/src/Imgix/UrlBuilder.php +++ b/src/Imgix/UrlBuilder.php @@ -25,9 +25,6 @@ class UrlBuilder { const MAX_WIDTH = 8192; const SRCSET_WIDTH_TOLERANCE = 0.08; - // constants cannot be dynamically assigned; keeping as a class variable instead - private $targetWidths; - public function __construct($domain, $useHttps = true, $signKey = "", $includeLibraryParam = true) { if (!is_string($domain)) { @@ -40,7 +37,6 @@ public function __construct($domain, $useHttps = true, $signKey = "", $includeLi $this->useHttps = $useHttps; $this->signKey = $signKey; $this->includeLibraryParam = $includeLibraryParam; - $this->targetWidths = $this->targetWidths(); } private function validateDomain($domain) {