From 522df7f954124e5d834ddedd11d4ca58c679c07f Mon Sep 17 00:00:00 2001 From: siad007 Date: Mon, 5 Apr 2021 22:13:52 +0200 Subject: [PATCH] Scrutinizer issue fixes --- src/Phing/Task/System/TouchTask.php | 130 +++++++++++++--------- tests/Phing/Task/System/TouchTaskTest.php | 38 +++---- 2 files changed, 95 insertions(+), 73 deletions(-) diff --git a/src/Phing/Task/System/TouchTask.php b/src/Phing/Task/System/TouchTask.php index d43f9fc2fc..cecefc56cb 100644 --- a/src/Phing/Task/System/TouchTask.php +++ b/src/Phing/Task/System/TouchTask.php @@ -46,24 +46,18 @@ class TouchTask extends Task private $file; private $seconds = -1; private $dateTime; - private $fileUtils; private $mkdirs = false; private $verbose = true; /** @var Mapper */ private $mapperElement; - public function __construct() - { - parent::__construct(); - $this->fileUtils = new FileUtils(); - } - /** * Sets a single source file to touch. If the file does not exist * an empty file will be created. + * @param File $file */ - public function setFile(File $file) + public function setFile(File $file): void { $this->file = $file; } @@ -78,7 +72,7 @@ public function setFile(File $file) * * @param int $millis */ - public function setMillis($millis) + public function setMillis(int $millis): void { if ($millis >= 0) { if ($millis >= 1000) { @@ -98,7 +92,7 @@ public function setMillis($millis) * * @param int $seconds */ - public function setSeconds($seconds) + public function setSeconds($seconds): void { if ($seconds >= 0) { $this->seconds = (int) $seconds; @@ -114,14 +108,18 @@ public function setSeconds($seconds) * * @param string $dateTime */ - public function setDatetime($dateTime) + public function setDatetime($dateTime): void { $timestmap = strtotime($dateTime); if (false !== $timestmap) { $this->dateTime = (string) $dateTime; $this->setSeconds($timestmap); } else { - throw new BuildException("Date of {$dateTime} cannot be parsed correctly. It should be in a format parsable by PHP's strtotime() function." . PHP_EOL); + throw new BuildException( + "Date of {$dateTime} cannot be parsed correctly. " + . "It should be in a format parsable by PHP's strtotime() function." + . PHP_EOL + ); } } @@ -131,7 +129,7 @@ public function setDatetime($dateTime) * * @param bool $mkdirs whether to create parent directories */ - public function setMkdirs($mkdirs) + public function setMkdirs($mkdirs): void { $this->mkdirs = $mkdirs; } @@ -142,7 +140,7 @@ public function setMkdirs($mkdirs) * * @param bool $verbose flag */ - public function setVerbose($verbose) + public function setVerbose($verbose): void { $this->verbose = $verbose; } @@ -153,7 +151,7 @@ public function setVerbose($verbose) * @throws BuildException * @throws IOException */ - public function createMapper() + public function createMapper(): Mapper { if (null !== $this->mapperElement) { throw new BuildException('Cannot define more than one mapper', $this->getLocation()); @@ -167,6 +165,7 @@ public function createMapper() * Execute the touch operation. * * @throws BuildException + * @throws IOException */ public function main() { @@ -174,7 +173,10 @@ public function main() $this->touch(); } - protected function checkConfiguration() + /** + * @throws IOException + */ + protected function checkConfiguration(): void { $savedSeconds = $this->seconds; @@ -196,8 +198,10 @@ protected function checkConfiguration() /** * Does the actual work. + * @throws IOException + * @throws \Exception */ - private function touch() + private function touch(): void { if (null !== $this->file) { if (!$this->file->exists()) { @@ -229,52 +233,25 @@ private function touch() $this->touchFile($this->file); } - $project = $this->getProject(); - foreach ($this->filesets as $fs) { - $ds = $fs->getDirectoryScanner($project); - $fromDir = $fs->getDir($project); - - $srcFiles = $ds->getIncludedFiles(); - $srcDirs = $ds->getIncludedDirectories(); - - for ($j = 0, $_j = count($srcFiles); $j < $_j; ++$j) { - foreach ($this->getMappedFileNames((string) $srcFiles[$j]) as $fileName) { - $this->touchFile(new File($fromDir, $fileName)); - } - } - - for ($j = 0, $_j = count($srcDirs); $j < $_j; ++$j) { - foreach ($this->getMappedFileNames((string) $srcDirs[$j]) as $fileName) { - $this->touchFile(new File($fromDir, $fileName)); - } - } - } - - // deal with the filelists - foreach ($this->filelists as $fl) { - $fromDir = $fl->getDir($this->getProject()); - - $srcFiles = $fl->getFiles($this->getProject()); - - for ($j = 0, $_j = count($srcFiles); $j < $_j; ++$j) { - foreach ($this->getMappedFileNames((string) $srcFiles[$j]) as $fileName) { - $this->touchFile(new File($fromDir, $fileName)); - } - } - } + $this->processFileSets(); + $this->processFileLists(); if ($resetSeconds) { $this->seconds = -1; } } - private function getMappedFileNames($file) + /** + * @param $file + * @return array + */ + private function getMappedFileNames($file): array { if (null !== $this->mapperElement) { $mapper = $this->mapperElement->getImplementation(); $results = $mapper->main($file); if (null === $results) { - return ''; + return []; } $fileNames = $results; } else { @@ -285,13 +262,58 @@ private function getMappedFileNames($file) } /** - * @throws BuildException + * @param File $file + * @throws \Exception */ - private function touchFile(File $file) + private function touchFile(File $file): void { if (!$file->canWrite()) { throw new BuildException('Can not change modification date of read-only file ' . (string) $file); } $file->setLastModified($this->seconds); } + + /** + * @throws IOException + * @throws \Exception + */ + private function processFileSets(): void + { + foreach ($this->filesets as $fs) { + $ds = $fs->getDirectoryScanner($this->getProject()); + $fromDir = $fs->getDir($this->getProject()); + + $srcFiles = $ds->getIncludedFiles(); + $srcDirs = $ds->getIncludedDirectories(); + + for ($j = 0, $_j = count($srcFiles); $j < $_j; ++$j) { + foreach ($this->getMappedFileNames((string) $srcFiles[$j]) as $fileName) { + $this->touchFile(new File($fromDir, $fileName)); + } + } + + for ($j = 0, $_j = count($srcDirs); $j < $_j; ++$j) { + foreach ($this->getMappedFileNames((string) $srcDirs[$j]) as $fileName) { + $this->touchFile(new File($fromDir, $fileName)); + } + } + } + } + + /** + * @throws IOException + * @throws \Exception + */ + private function processFileLists(): void + { + foreach ($this->filelists as $fl) { + $fromDir = $fl->getDir($this->getProject()); + $srcFiles = $fl->getFiles($this->getProject()); + foreach ($srcFiles as $jValue) { + foreach ($this->getMappedFileNames((string) $jValue) as $fileName) { + $this->touchFile(new File($fromDir, $fileName)); + } + } + } + } } diff --git a/tests/Phing/Task/System/TouchTaskTest.php b/tests/Phing/Task/System/TouchTaskTest.php index 490c1820a1..acaa4a28eb 100644 --- a/tests/Phing/Task/System/TouchTaskTest.php +++ b/tests/Phing/Task/System/TouchTaskTest.php @@ -45,7 +45,7 @@ public function tearDown(): void $this->executeTarget('clean'); } - public function testSimpleTouch() + public function testSimpleTouch(): void { $this->executeTarget(__FUNCTION__); $this->assertFileExists( @@ -54,7 +54,7 @@ public function testSimpleTouch() ); } - public function testMkdirs() + public function testMkdirs(): void { $this->executeTarget(__FUNCTION__); $this->assertFileExists( @@ -63,7 +63,7 @@ public function testMkdirs() ); } - public function testMkdirsFails() + public function testMkdirsFails(): void { $this->expectException(BuildException::class); $this->expectExceptionMessage('Error creating new file'); @@ -76,7 +76,7 @@ public function testMkdirsFails() ); } - public function testFilelist() + public function testFilelist(): void { $this->executeTarget(__FUNCTION__); $this->assertFileExists( @@ -85,7 +85,7 @@ public function testFilelist() ); } - public function testFileset() + public function testFileset(): void { $this->executeTarget(__FUNCTION__); $this->assertFileExists( @@ -94,7 +94,7 @@ public function testFileset() ); } - public function testMappedFileset() + public function testMappedFileset(): void { $this->executeTarget(__FUNCTION__); $tmpDir = $this->getProject()->getProperty('tmp.dir'); @@ -106,7 +106,7 @@ public function testMappedFileset() /** * test the mapped file list. */ - public function testMappedFilelist() + public function testMappedFilelist(): void { $this->executeTarget(__FUNCTION__); $tmpDir = $this->getProject()->getProperty('tmp.dir'); @@ -116,7 +116,7 @@ public function testMappedFilelist() /** * test millis attribute. */ - public function testMillis() + public function testMillis(): void { // Don't run the test on 32-bit systems if (PHP_INT_SIZE > 4) { @@ -133,7 +133,7 @@ public function testMillis() /** * test seconds attribute. */ - public function testSeconds() + public function testSeconds(): void { $this->executeTarget(__FUNCTION__); $testFile = $this->getProject()->getProperty('tmp.dir') . '/seconds-file'; @@ -145,7 +145,7 @@ public function testSeconds() /** * test datetime attribute. */ - public function testDatetime() + public function testDatetime(): void { $this->executeTarget(__FUNCTION__); $testFile = $this->getProject()->getProperty('tmp.dir') . '/datetime-file'; @@ -157,27 +157,27 @@ public function testDatetime() /** * test datetime with improper datetime. */ - public function testNotDateTime() + public function testNotDateTime(): void { $this->expectBuildException(__FUNCTION__, 'when datetime has invalid value'); } - public function testNoFile() + public function testNoFile(): void { $this->expectBuildException(__FUNCTION__, 'when no file specified'); } - public function testFileIsDirectory() + public function testFileIsDirectory(): void { $this->expectBuildException(__FUNCTION__, 'when file specified is a directory'); } - public function testDatetimePreEpoch() + public function testDatetimePreEpoch(): void { $this->expectBuildException(__FUNCTION__, 'when datetime is prior to January 1, 1970'); } - public function testReadOnlyFile() + public function testReadOnlyFile(): void { $readOnlyFile = $this->getProject()->getProperty('tmp.dir') . '/readonly-file'; if (file_exists($readOnlyFile)) { @@ -204,22 +204,22 @@ public function testReadOnlyFile() } } - public function testMillisNegative() + public function testMillisNegative(): void { $this->expectBuildException(__FUNCTION__, 'when millis is negative'); } - public function testSecondsNegative() + public function testSecondsNegative(): void { $this->expectBuildException(__FUNCTION__, 'when seconds is negative'); } - public function testMillisSubSecond() + public function testMillisSubSecond(): void { $this->expectBuildException(__FUNCTION__, 'when millis is less than a second'); } - public function testDefaultToNow() + public function testDefaultToNow(): void { $nowTime = time();