From 10f00d7c83fc360469f61ac37801d4e540b2e8f3 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Fri, 24 Mar 2023 14:54:55 +0100 Subject: [PATCH 1/3] many, small improvements proposed by phpstorm --- CHANGELOG.md | 4 ++++ README.md | 18 +++++++++--------- config/bootstrap.php | 7 ++++++- src/TestSuite/TestCase.php | 2 +- src/Utility/LinkScanner.php | 4 ++-- .../Command/LinkScannerCommandTest.php | 6 +++--- tests/TestCase/Utility/LinkScannerTest.php | 4 ++-- tests/bootstrap.php | 4 ++-- version | 2 +- 9 files changed, 30 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67798ad..8c72949 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # 1.x branch ## 1.1 branch +### 1.1.16 +* added the `LINK_SCANNER_TMP` constant. This constant, which can be defined before the plugin, affects all used files; +* many, small improvements proposed by phpstorm. + ### 1.1.15 * the `LinkScannerCommand` also prints the total number of invalid links to the console; * many, small code tweaks proposed by phpstorm; diff --git a/README.md b/README.md index e21da04..d4654d2 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Did you like this plugin? Its development requires a lot of time for me. Please consider the possibility of making [a donation](//paypal.me/mirkopagliai): even a coffee is enough! Thank you. -[![Make a donation](https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_carte.jpg)](//paypal.me/mirkopagliai) +[![Make a donation](https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_carte.jpg)](https://paypal.me/mirkopagliai) *** @@ -36,7 +36,7 @@ $ composer require --prefer-dist mirko-pagliai/cakephp-link-scanner ``` Then you have to load the plugin. For more information on how to load the plugin, -please refer to the [Cookbook](//book.cakephp.org/4.0/en/plugins.html#loading-a-plugin). +please refer to the [Cookbook](https://book.cakephp.org/4.0/en/plugins.html#loading-a-plugin). Simply, you can execute the shell command to enable the plugin: ```bash @@ -52,7 +52,7 @@ However, there are still some branches compatible with previous versions of CakePHP and PHP. #### For PHP 7.2 or later -The [php7.2](//github.com/mirko-pagliai/cakephp-link-scanner/tree/php7.2) branch +The [php7.2](https://github.com/mirko-pagliai/cakephp-link-scanner/tree/php7.2) branch requires at least PHP 7.2. In this case, you can install the package as well: @@ -79,17 +79,17 @@ except for security patches, and it matches the ## Configuration It's not essential, but it may be useful to set the `App.fullBaseUrl` value -correctly [refer to the Cookbook](//book.cakephp.org/4.0/en/development/configuration.html#general-configuration), +correctly [refer to the Cookbook](https://book.cakephp.org/4.0/en/development/configuration.html#general-configuration), especially if you plan to use the plugin mainly on your app, so as not to have to indicate the full base url which to start the scan every time. ## How to use Please, refer to the wiki: -* [How to use the LinkScanner utility](//github.com/mirko-pagliai/cakephp-link-scanner/wiki/How-to-use-the-LinkScanner-utility) -* [How to use the LinkScannerCommand](//github.com/mirko-pagliai/cakephp-link-scanner/wiki/How-to-use-the-LinkScannerCommand) -* [Examples for ResultScan](//github.com/mirko-pagliai/cakephp-link-scanner/wiki/Examples-for-ResultScan) +* [How to use the LinkScanner utility](https://github.com/mirko-pagliai/cakephp-link-scanner/wiki/How-to-use-the-LinkScanner-utility) +* [How to use the LinkScannerCommand](https://github.com/mirko-pagliai/cakephp-link-scanner/wiki/How-to-use-the-LinkScannerCommand) +* [Examples for ResultScan](https://github.com/mirko-pagliai/cakephp-link-scanner/wiki/Examples-for-ResultScan) -In addition, you can refer to our [API](//mirko-pagliai.github.io/cakephp-link-scanner). +In addition, you can refer to our [API](https://mirko-pagliai.github.io/cakephp-link-scanner). ## To do list * allow the use of a configuration file for the shell; @@ -98,4 +98,4 @@ In addition, you can refer to our [API](//mirko-pagliai.github.io/cakephp-link-s ## Versioning For transparency and insight into our release cycle and to maintain backward compatibility, *Assets* will be maintained under the -[Semantic Versioning guidelines](//semver.org). +[Semantic Versioning guidelines](https://semver.org). diff --git a/config/bootstrap.php b/config/bootstrap.php index fc87a42..57deeee 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -15,11 +15,16 @@ use Cake\Cache\Cache; +//Default LinkScanner tmp +if (!defined('LINK_SCANNER_TMP')) { + define('LINK_SCANNER_TMP', TMP . 'cakephp-link-scanner' . DS); +} + if (!Cache::getConfig('LinkScanner')) { Cache::setConfig('LinkScanner', [ 'className' => 'File', 'duration' => '+1 day', - 'path' => CACHE, + 'path' => LINK_SCANNER_TMP . 'cache' . DS, 'prefix' => 'link_scanner_', ]); } diff --git a/src/TestSuite/TestCase.php b/src/TestSuite/TestCase.php index 10e85bb..b6cb94f 100644 --- a/src/TestSuite/TestCase.php +++ b/src/TestSuite/TestCase.php @@ -41,7 +41,7 @@ public function tearDown(): void { parent::tearDown(); - @unlink(TMP . 'cakephp-link-scanner' . DS . 'link_scanner_lock_file'); + @unlink(LINK_SCANNER_TMP . 'link_scanner_lock_file'); Cache::clear('LinkScanner'); } diff --git a/src/Utility/LinkScanner.php b/src/Utility/LinkScanner.php index b0dbc98..b5e9358 100644 --- a/src/Utility/LinkScanner.php +++ b/src/Utility/LinkScanner.php @@ -67,7 +67,7 @@ class LinkScanner implements Serializable 'fullBaseUrl' => null, 'maxDepth' => 0, 'lockFile' => true, - 'target' => TMP . 'cakephp-link-scanner', + 'target' => LINK_SCANNER_TMP, ]; /** @@ -98,7 +98,7 @@ class LinkScanner implements Serializable * Lock file path * @var string */ - protected string $lockFile = TMP . 'cakephp-link-scanner' . DS . 'link_scanner_lock_file'; + protected string $lockFile = LINK_SCANNER_TMP . 'link_scanner_lock_file'; /** * Start time diff --git a/tests/TestCase/Command/LinkScannerCommandTest.php b/tests/TestCase/Command/LinkScannerCommandTest.php index 1dbd6eb..1a4c3de 100644 --- a/tests/TestCase/Command/LinkScannerCommandTest.php +++ b/tests/TestCase/Command/LinkScannerCommandTest.php @@ -154,7 +154,7 @@ public function testExecuteParams(): void $this->Command->run($params, $this->_io); $expectedDuration = Cache::getConfig('LinkScanner')['duration']; - $expectedFilename = $this->LinkScanner->getConfig('target') . DS . 'results_' . $this->LinkScanner->hostname . '_' . $this->LinkScanner->startTime; + $expectedFilename = $this->LinkScanner->getConfig('target') . 'results_' . $this->LinkScanner->hostname . '_' . $this->LinkScanner->startTime; $this->assertEquals($expectedConfig, $this->LinkScanner->getConfig()); $this->assertEquals(15, $this->LinkScanner->Client->getConfig('timeout')); @@ -195,7 +195,7 @@ public function testExecuteParams(): void //It also works without the `--export` parameter self::setUp(); $this->Command->run(array_merge(['--export-only-bad-results'] + $params), $this->_io); - $expectedFilename = $this->LinkScanner->getConfig('target') . DS . 'results_' . $this->LinkScanner->hostname . '_' . $this->LinkScanner->startTime; + $expectedFilename = $this->LinkScanner->getConfig('target') . 'results_' . $this->LinkScanner->hostname . '_' . $this->LinkScanner->startTime; $this->assertEquals(['exportOnlyBadResults' => true] + $expectedConfig, $this->LinkScanner->getConfig()); $this->assertOutputRegExp(sprintf('/Scan started for %s/', preg_quote($this->LinkScanner->getConfig('fullBaseUrl'), '/'))); $this->assertOutputRegExp('/Total scanned links\: [1-9]\d*/'); @@ -232,7 +232,7 @@ public function testExecuteParams(): void $this->assertNotEmpty(array_filter($this->_out->messages(), $differentLines)); foreach ([ - 'example' => $this->LinkScanner->getConfig('target') . DS . 'example', + 'example' => $this->LinkScanner->getConfig('target') . 'example', TMP . 'example' => TMP . 'example', ] as $filename => $expectedExportFile) { self::setUp(); diff --git a/tests/TestCase/Utility/LinkScannerTest.php b/tests/TestCase/Utility/LinkScannerTest.php index 58a5c00..6e1755c 100644 --- a/tests/TestCase/Utility/LinkScannerTest.php +++ b/tests/TestCase/Utility/LinkScannerTest.php @@ -172,8 +172,8 @@ public function testExport(): void //Filename can be `null`, relative or absolute foreach ([ - null => $target . DS . 'results_' . $this->LinkScanner->hostname . '_' . $this->LinkScanner->startTime, - 'example' => $target . DS . 'example', + null => $target . 'results_' . $this->LinkScanner->hostname . '_' . $this->LinkScanner->startTime, + 'example' => $target . 'example', TMP . 'example' => TMP . 'example', ] as $filename => $expectedFilename) { $result = $this->LinkScanner->export($filename); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 289b808..89d06d9 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -32,13 +32,13 @@ define('APP_DIR', 'TestApp'); define('WEBROOT_DIR', 'webroot'); define('WWW_ROOT', APP . 'webroot' . DS); -define('TMP', sys_get_temp_dir() . DS . 'cakephp-link-scanner-tmp' . DS); +define('TMP', sys_get_temp_dir() . DS); define('CACHE', TMP . 'cache' . DS); define('CONFIG', APP . 'config' . DS); define('LOGS', TMP . 'logs' . DS); define('SESSIONS', TMP . 'sessions' . DS); -foreach ([TMP, LOGS, SESSIONS, CACHE,CACHE . 'views', CACHE . 'models'] as $dir) { +foreach (array_filter([TMP, LOGS, SESSIONS, CACHE, CACHE . 'views', CACHE . 'models'], fn(string $dir): bool => !file_exists($dir)) as $dir) { mkdir($dir); } diff --git a/version b/version index 645377e..63b283b 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.1.15 +1.1.16 From f9dc4521b2c14a0989ba3d7d275d7492ecea9daa Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Fri, 24 Mar 2023 16:53:49 +0100 Subject: [PATCH 2/3] updated --- psalm-baseline.neon | 8 ++++++++ psalm.xml | 5 +---- src/Command/LinkScannerCommand.php | 4 +++- tests/bootstrap.php | 10 +++++----- 4 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 psalm-baseline.neon diff --git a/psalm-baseline.neon b/psalm-baseline.neon new file mode 100644 index 0000000..9ab32bd --- /dev/null +++ b/psalm-baseline.neon @@ -0,0 +1,8 @@ + + + + + empty($this->LinkScanner) + + + diff --git a/psalm.xml b/psalm.xml index 53ce931..b9ba587 100644 --- a/psalm.xml +++ b/psalm.xml @@ -6,6 +6,7 @@ xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" autoloader="tests/bootstrap.php" + errorBaseline="psalm-baseline.neon" > @@ -13,8 +14,4 @@ - - - - diff --git a/src/Command/LinkScannerCommand.php b/src/Command/LinkScannerCommand.php index a792bb7..8f34c0c 100644 --- a/src/Command/LinkScannerCommand.php +++ b/src/Command/LinkScannerCommand.php @@ -40,7 +40,9 @@ class LinkScannerCommand extends Command */ public function initialize(): void { - $this->LinkScanner ??= new LinkScanner(); + if (empty($this->LinkScanner)) { + $this->LinkScanner = new LinkScanner(); + } } /** diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 89d06d9..873415d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -23,7 +23,6 @@ define('ROOT', dirname(__DIR__) . DS); define('VENDOR', ROOT . 'vendor' . DS); -define('CAKE_CORE_INCLUDE_PATH', ROOT . 'vendor' . DS . 'cakephp' . DS . 'cakephp'); define('CORE_PATH', ROOT . 'vendor' . DS . 'cakephp' . DS . 'cakephp' . DS); define('CAKE', CORE_PATH . 'src' . DS); define('TESTS', ROOT . 'tests' . DS); @@ -38,12 +37,13 @@ define('LOGS', TMP . 'logs' . DS); define('SESSIONS', TMP . 'sessions' . DS); -foreach (array_filter([TMP, LOGS, SESSIONS, CACHE, CACHE . 'views', CACHE . 'models'], fn(string $dir): bool => !file_exists($dir)) as $dir) { - mkdir($dir); -} - require dirname(__DIR__) . '/vendor/autoload.php'; require CORE_PATH . 'config' . DS . 'bootstrap.php'; +require ROOT . 'config' . DS . 'bootstrap.php'; + +foreach (array_filter([LINK_SCANNER_TMP, LOGS, SESSIONS, CACHE . 'views', CACHE . 'models'], fn(string $dir): bool => !file_exists($dir)) as $dir) { + mkdir($dir, 0777, true); +} Configure::write('debug', true); Configure::write('App', [ From 6c7ec4d1456403542f28c7e0fdcf0e5dd46d5a73 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Fri, 24 Mar 2023 19:02:42 +0100 Subject: [PATCH 3/3] come back --- psalm-baseline.neon | 7 ++++--- src/Command/LinkScannerCommand.php | 4 +--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/psalm-baseline.neon b/psalm-baseline.neon index 9ab32bd..f685dc0 100644 --- a/psalm-baseline.neon +++ b/psalm-baseline.neon @@ -1,8 +1,9 @@ - - empty($this->LinkScanner) - + + $this->LinkScanner + new LinkScanner() + diff --git a/src/Command/LinkScannerCommand.php b/src/Command/LinkScannerCommand.php index 8f34c0c..a792bb7 100644 --- a/src/Command/LinkScannerCommand.php +++ b/src/Command/LinkScannerCommand.php @@ -40,9 +40,7 @@ class LinkScannerCommand extends Command */ public function initialize(): void { - if (empty($this->LinkScanner)) { - $this->LinkScanner = new LinkScanner(); - } + $this->LinkScanner ??= new LinkScanner(); } /**