Skip to content

Commit

Permalink
Scrutinizer issue fixes (#1579)
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 authored Apr 5, 2021
1 parent 56ab68e commit 1377a93
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 73 deletions.
130 changes: 76 additions & 54 deletions src/Phing/Task/System/TouchTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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
);
}
}

Expand All @@ -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;
}
Expand All @@ -142,7 +140,7 @@ public function setMkdirs($mkdirs)
*
* @param bool $verbose flag
*/
public function setVerbose($verbose)
public function setVerbose($verbose): void
{
$this->verbose = $verbose;
}
Expand All @@ -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());
Expand All @@ -167,14 +165,18 @@ public function createMapper()
* Execute the touch operation.
*
* @throws BuildException
* @throws IOException
*/
public function main()
{
$this->checkConfiguration();
$this->touch();
}

protected function checkConfiguration()
/**
* @throws IOException
*/
protected function checkConfiguration(): void
{
$savedSeconds = $this->seconds;

Expand All @@ -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()) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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));
}
}
}
}
}
38 changes: 19 additions & 19 deletions tests/Phing/Task/System/TouchTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function tearDown(): void
$this->executeTarget('clean');
}

public function testSimpleTouch()
public function testSimpleTouch(): void
{
$this->executeTarget(__FUNCTION__);
$this->assertFileExists(
Expand All @@ -54,7 +54,7 @@ public function testSimpleTouch()
);
}

public function testMkdirs()
public function testMkdirs(): void
{
$this->executeTarget(__FUNCTION__);
$this->assertFileExists(
Expand All @@ -63,7 +63,7 @@ public function testMkdirs()
);
}

public function testMkdirsFails()
public function testMkdirsFails(): void
{
$this->expectException(BuildException::class);
$this->expectExceptionMessage('Error creating new file');
Expand All @@ -76,7 +76,7 @@ public function testMkdirsFails()
);
}

public function testFilelist()
public function testFilelist(): void
{
$this->executeTarget(__FUNCTION__);
$this->assertFileExists(
Expand All @@ -85,7 +85,7 @@ public function testFilelist()
);
}

public function testFileset()
public function testFileset(): void
{
$this->executeTarget(__FUNCTION__);
$this->assertFileExists(
Expand All @@ -94,7 +94,7 @@ public function testFileset()
);
}

public function testMappedFileset()
public function testMappedFileset(): void
{
$this->executeTarget(__FUNCTION__);
$tmpDir = $this->getProject()->getProperty('tmp.dir');
Expand All @@ -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');
Expand All @@ -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) {
Expand All @@ -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';
Expand All @@ -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';
Expand All @@ -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)) {
Expand All @@ -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();

Expand Down

0 comments on commit 1377a93

Please sign in to comment.