diff --git a/.travis.yml b/.travis.yml index 86026c5..d323301 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,43 +1,36 @@ language: php +cache: + directories: + - $HOME/.composer/cache/files + - .phpunit + +env: + global: + - SYMFONY_PHPUNIT_DIR=.phpunit + matrix: + fast_finish: true include: - - php: 5.6 - env: SYMFONY_VERSION=2.8.* - - php: 5.6 - env: SYMFONY_VERSION=3.0.* - - php: 5.6 - env: SYMFONY_VERSION=3.1.* - - php: 5.6 - env: SYMFONY_VERSION=3.2.* - - php: 5.6 - env: SYMFONY_VERSION=3.3.* - - php: 5.6 - env: SYMFONY_VERSION=3.4.* - - php: 7.0 - env: SYMFONY_VERSION=2.8.* - - php: 7.0 - env: SYMFONY_VERSION=3.0.* - - php: 7.0 - env: SYMFONY_VERSION=3.1.* - - php: 7.0 - env: SYMFONY_VERSION=3.2.* - - php: 7.0 - env: SYMFONY_VERSION=3.4.* - - php: 7.1 - env: SYMFONY_VERSION=3.4.* - - php: 7.1 - env: SYMFONY_VERSION=4.0.* - - php: 7.1 - env: SYMFONY_VERSION=4.1.* - - php: 7.2 - env: SYMFONY_VERSION=3.4.* - - php: 7.2 - env: SYMFONY_VERSION=4.0.* - - php: 7.2 - env: SYMFONY_VERSION=4.1.* + - php: 7.3 + env: SYMFONY_VERSION="3.4.*" + - php: 7.3 + env: SYMFONY_VERSION="4.4.*" + - php: 7.3 + env: SYMFONY_VERSION="5.0.*" + - php: 7.4 + env: SYMFONY_VERSION="3.4.*" + - php: 7.4 + env: SYMFONY_VERSION="4.4.*" + - php: 7.4 + env: SYMFONY_VERSION="5.0.*" + +before_install: + - composer self-update + - composer validate + - if [ "$SYMFONY_VERSION" != "" ]; then composer require --no-update symfony/process:"${SYMFONY_VERSION}"; fi -before_script: - - composer require symfony/process:${SYMFONY_VERSION} +install: + - composer update $COMPOSER_FLAGS -script: bin/phpunit +script: bin/phpunit -v diff --git a/LICENSE b/LICENSE index 53bcd66..082fede 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2013-2017 MyBuilder Limited +Copyright (c) 2013-2020 MyBuilder Limited Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -12,7 +12,7 @@ all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN diff --git a/README.md b/README.md index 2884eb4..ed41752 100644 --- a/README.md +++ b/README.md @@ -4,26 +4,18 @@ Easily configure cron through PHP. -If you use Symfony 2, you could use our [cool bundle](https://github.com/mybuilder/cronos-bundle) in order to configure your app jobs through fancy annotations! +If you use Symfony 3/4/5, you could use our [cool bundle](https://github.com/mybuilder/cronos-bundle) in order to configure your app jobs through fancy annotations! ## Setup and Configuration -Add the following to your `composer.json` file -```json -{ - "require": { - "mybuilder/cronos": "~1.0" - } -} -``` -Update the vendor libraries +Require the library via composer: - curl -s http://getcomposer.org/installer | php - php composer.phar install + composer require mybuilder/cronos ## Usage ### Build Cron + ```php replaceWith($cron); ## Troubleshooting -* The current user must have a existing crontab file to use the updater, use crontab -e to create one. +* The current user must have a existing crontab file to use the updater, use `crontab -e` to create one. * When a cron line is executed it is executed with the user that owns the crontab, but it will not execute any of the users default shell files so all paths etc need to be specified in the command called from the cron line. -* Your crontab will not be executed if you do not have useable shell in /etc/passwd -* If your jobs don't seem to be running check that the cron deamon is running, also check your username is in /etc/cron.allow and not in /etc/cron.deny. -* Environmental substitutions do not work, you can not use things like $PATH, $HOME, or ~/sbin. -* You can not use % in the command, if you need to use it escape the command in backticks. - +* Your crontab will not be executed if you do not have usable shell in `/etc/passwd` +* If your jobs don't seem to be running, check the cron daemon is running, also check your username is in `/etc/cron.allow` and not in `/etc/cron.deny`. +* Environmental substitutions do not work, you cannot use things like `$PATH`, `$HOME`, or `~/sbin`. +* You cannot use `%` in the command, if you need to use it, escape the command in backticks. --- diff --git a/composer.json b/composer.json index 75bd291..b0ea8d4 100644 --- a/composer.json +++ b/composer.json @@ -15,15 +15,15 @@ } ], "require": { - "php": ">=5.6.0 || ^7.0", - "symfony/process": "~2.2|~3.0|^4.0" + "php": ">=7.3", + "symfony/process": "~3.0|^4.0|^5.0" }, "suggest": { "symfony/filesystem": "Allows using Symfony Filesystem" }, "require-dev": { - "phpunit/phpunit": "~5.0", - "mockery/mockery": "~0.9" + "roave/security-advisories": "dev-master", + "phpunit/phpunit": "^9.0" }, "autoload": { "psr-4": { "MyBuilder\\Cronos\\": "src" } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e08c9a0..3965e66 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,25 +2,19 @@ + backupGlobals="false" + backupStaticAttributes="false" + colors="true" + convertErrorsToExceptions="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + processIsolation="false" + stopOnFailure="true" + bootstrap="./vendor/autoload.php"> tests - - - - - diff --git a/src/Formatter/Comment.php b/src/Formatter/Comment.php index 1740cbd..2d1d789 100644 --- a/src/Formatter/Comment.php +++ b/src/Formatter/Comment.php @@ -4,29 +4,21 @@ class Comment { - /** - * @var string - */ + /** @var string */ private $comment; - /** - * @param string $comment - */ - public function __construct($comment) + public function __construct(string $comment) { $this->comment = $comment; } - /** - * @return string - */ - public function format() + public function format(): string { return '#' . $this->removeLineBreaks($this->comment); } - private function removeLineBreaks($text) + private function removeLineBreaks($text): string { - return str_replace(array("\r", "\r\n", "\n", PHP_EOL), '', $text); + return \str_replace(["\r", "\r\n", "\n", \PHP_EOL], '', $text); } } diff --git a/src/Formatter/Cron.php b/src/Formatter/Cron.php index 5d59cdf..27667d8 100644 --- a/src/Formatter/Cron.php +++ b/src/Formatter/Cron.php @@ -4,32 +4,20 @@ class Cron { - /** - * @var Header - */ + /** @var Header */ private $header; - /** - * @var mixed[] - */ - private $lines = array(); + /** @var mixed[] */ + private $lines = []; - /** - * @return Header - */ - public function header() + public function header(): Header { $this->header = new Header($this); return $this->header; } - /** - * @param string $command - * - * @return Job - */ - public function job($command) + public function job(string $command): Job { $line = new Job($command, $this); $this->lines[] = $line; @@ -37,40 +25,30 @@ public function job($command) return $line; } - /** - * @param string $comment - * - * @return Cron - */ - public function comment($comment) + public function comment(string $comment): Cron { $this->lines[] = new Comment($comment); return $this; } - /** - * @return int - */ - public function countLines() + public function countLines(): int { return count($this->lines); } - /** - * @return string - */ - public function format() + public function format(): string { $lines = ''; + foreach ($this->lines as $line) { - $lines .= $line->format() . PHP_EOL; + $lines .= $line->format() . \PHP_EOL; } - return (($this->hasHeader()) ? $this->header->format() . PHP_EOL : '') . trim($lines) . PHP_EOL; + return (($this->hasHeader()) ? $this->header->format() . \PHP_EOL : '') . \trim($lines) . \PHP_EOL; } - public function hasHeader() + public function hasHeader(): bool { return $this->header !== null; } diff --git a/src/Formatter/Header.php b/src/Formatter/Header.php index bec2b5e..7055867 100644 --- a/src/Formatter/Header.php +++ b/src/Formatter/Header.php @@ -4,49 +4,30 @@ class Header { - /** - * @var Cron - */ + /** @var Cron */ private $cron; - /** - * @var string - */ + /** @var string */ private $path; - /** - * @var string - */ + /** @var string */ private $mailTo; - /** - * @var string - */ + /** @var string */ private $home; - /** - * @var string - */ + /** @var string */ private $shell; - /** - * @var string - */ + /** @var string */ private $contentType; - /** - * @var string - */ + /** @var string */ private $encoding; - /** - * @var string - */ + /** @var string */ private $timezone; - /** - * @param Cron $cron - */ public function __construct(Cron $cron) { $this->cron = $cron; @@ -54,38 +35,23 @@ public function __construct(Cron $cron) /** * Works just like the shell PATH, but it does not inherit from your environment. - * - * @param string $path - * - * @return $this */ - public function setPath($path) + public function setPath(string $path): self { $this->path = $path; return $this; } - /** - * @param string $home - * - * @return $this - */ - public function setHome($home) + public function setHome(string $home): self { $this->home = $home; return $this; } - /** - * @param string $email - * - * @return $this - * - * @throws InvalidEmail if given email is invalid - */ - public function setMailto($email) + /** @throws InvalidEmail if given email is invalid */ + public function setMailto(string $email): self { $this->assertValidEmail($email); $this->mailTo = $email; @@ -93,9 +59,9 @@ public function setMailto($email) return $this; } - private function assertValidEmail($email) + private function assertValidEmail($email): void { - if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + if (false === \filter_var($email, \FILTER_VALIDATE_EMAIL)) { throw new InvalidEmail($email); } } @@ -104,12 +70,8 @@ private function assertValidEmail($email) * Set the shell to be used when executing commands * * Default is /bin/sh but can also be changed to /bin/php - * - * @param string $shell - * - * @return $this */ - public function setShell($shell) + public function setShell(string $shell): self { $this->shell = $shell; @@ -118,12 +80,8 @@ public function setShell($shell) /** * Set the content-type to use for cron output emails. - * - * @param string $contentType - * - * @return $this */ - public function setContentType($contentType) + public function setContentType(string $contentType): self { $this->contentType = $contentType; @@ -132,36 +90,25 @@ public function setContentType($contentType) /** * Set the charset to use for cron output emails. - * - * @param string $encoding - * - * @return $this */ - public function setContentTransferEncoding($encoding) + public function setContentTransferEncoding(string $encoding): self { $this->encoding = $encoding; return $this; } - /** - * @param $timezone - * - * @return $this - */ - public function setTimezone($timezone) + public function setTimezone(string $timezone): self { $this->timezone = $timezone; return $this; } - /** - * @return string - */ - public function format() + public function format(): string { $headers = ''; + if ($this->path) { $headers .= $this->createHeader('PATH', $this->path); } @@ -187,15 +134,12 @@ public function format() return $headers; } - private function createHeader($name, $value) + private function createHeader($name, $value): string { - return $name . '=' . $value . PHP_EOL; + return $name . '=' . $value . \PHP_EOL; } - /** - * @return Cron - */ - public function end() + public function end(): Cron { return $this->cron; } diff --git a/src/Formatter/Job.php b/src/Formatter/Job.php index 9477e8b..ad68935 100644 --- a/src/Formatter/Job.php +++ b/src/Formatter/Job.php @@ -4,31 +4,19 @@ class Job { - /** - * @var Cron - */ + /** @var Cron */ private $cron; - /** - * @var Time - */ + /** @var Time */ private $time; - /** - * @var string - */ + /** @var string */ private $command; - /** - * @var Output - */ + /** @var Output */ private $output; - /** - * @param string $command - * @param Cron $cron - */ - public function __construct($command, Cron $cron) + public function __construct(string $command, Cron $cron) { $this->cron = $cron; $this->time = new Time; @@ -36,70 +24,40 @@ public function __construct($command, Cron $cron) $this->output = new Output; } - /** - * @param string $value - * - * @return $this - * - * @see Time::setMinute - */ - public function setMinute($value) + /** @see Time::setMinute */ + public function setMinute(string $value): self { $this->time->setMinute($value); return $this; } - /** - * @param string $value - * - * @return $this - * - * @see Time::setHour - */ - public function setHour($value) + /** @see Time::setHour */ + public function setHour(string $value): self { $this->time->setHour($value); return $this; } - /** - * @param string $value - * - * @return $this - * - * @see Time::setDayOfMonth - */ - public function setDayOfMonth($value) + /** @see Time::setDayOfMonth */ + public function setDayOfMonth(string $value): self { $this->time->setDayOfMonth($value); return $this; } - /** - * @param string $value - * - * @return $this - * - * @see Time::setMonth - */ - public function setMonth($value) + /** @see Time::setMonth */ + public function setMonth(string $value): self { $this->time->setMonth($value); return $this; } - /** - * @param string $value - * - * @return $this - * - * @see Time::setDayOfWeek - */ - public function setDayOfWeek($value) + /** @see Time::setDayOfWeek */ + public function setDayOfWeek(string$value): self { $this->time->setDayOfWeek($value); @@ -109,90 +67,58 @@ public function setDayOfWeek($value) /** * Suppress the output of this command when executed * - * @return $this - * * @see Output::suppressOutput */ - public function suppressOutput() + public function suppressOutput(): self { $this->output->suppressOutput(); return $this; } - /** - * @param string $filePath - * - * @return $this - * - * @see Output::setStandardOutFile - */ - public function setStandardOutFile($filePath) + /** @see Output::setStandardOutFile */ + public function setStandardOutFile(string $filePath): self { $this->output->setStandardOutFile($filePath); return $this; } - /** - * @param string $filePath - * - * @return $this - * - * @see Output::appendStandardOutToFile - */ - public function appendStandardOutToFile($filePath) + /** @see Output::appendStandardOutToFile */ + public function appendStandardOutToFile(string $filePath): self { $this->output->appendStandardOutToFile($filePath); return $this; } - /** - * @param string $filePath - * - * @return $this - * - * @see Output::setStandardErrorFile - */ - public function setStandardErrorFile($filePath) + /** @see Output::setStandardErrorFile */ + public function setStandardErrorFile(string $filePath): self { $this->output->setStandardErrorFile($filePath); return $this; } - /** - * @param string $filePath - * - * @return $this - * - * @see Output::appendStandardErrorToFile - */ - public function appendStandardErrorToFile($filePath) + /** @see Output::appendStandardErrorToFile */ + public function appendStandardErrorToFile(string $filePath): self { $this->output->appendStandardErrorToFile($filePath); return $this; } - /** - * @return Cron - */ - public function end() + public function end(): Cron { return $this->cron; } - /** - * @return string - */ - public function format() + public function format(): string { return $this->time->format() . $this->command . $this->output->format() . - PHP_EOL; + \PHP_EOL; } } diff --git a/src/Formatter/Output.php b/src/Formatter/Output.php index 34b6794..f63f204 100644 --- a/src/Formatter/Output.php +++ b/src/Formatter/Output.php @@ -4,49 +4,31 @@ class Output { - const NO_FILE = '/dev/null'; + public const NO_FILE = '/dev/null'; - /** - * @var boolean - */ + /** @var bool */ private $noOutput = false; - /** - * @var string - */ + /** @var string */ private $stdOutFile; - /** - * @var bool - */ + /** @var bool */ private $stdOutAppend; - /** - * @var string - */ + /** @var string */ private $stdErrFile; - /** - * @var bool - */ + /** @var bool */ private $stdErrAppend; - /** - * @return $this - */ - public function suppressOutput() + public function suppressOutput(): self { $this->noOutput = true; return $this; } - /** - * @param string $filePath - * - * @return $this - */ - public function setStandardOutFile($filePath) + public function setStandardOutFile(string $filePath): self { $this->stdOutFile = $filePath; $this->stdOutAppend = false; @@ -54,12 +36,8 @@ public function setStandardOutFile($filePath) return $this; } - /** - * @param string $filePath - * - * @return $this - */ - public function appendStandardOutToFile($filePath) { + public function appendStandardOutToFile(string $filePath): self + { $this->stdOutFile = $filePath; $this->stdOutAppend = true; @@ -67,11 +45,9 @@ public function appendStandardOutToFile($filePath) { } /** - * @param string $filePath * @param bool $append Either append or rewrite log file - * @return $this */ - public function setStandardErrorFile($filePath, $append = false) + public function setStandardErrorFile(string $filePath, bool $append = false): self { $this->stdErrFile = $filePath; $this->stdErrAppend = $append; @@ -79,22 +55,15 @@ public function setStandardErrorFile($filePath, $append = false) return $this; } - /** - * @param string $filePath - * - * @return $this - */ - public function appendStandardErrorToFile($filePath) { + public function appendStandardErrorToFile(string $filePath): self + { $this->stdErrFile = $filePath; $this->stdErrAppend = true; return $this; } - /** - * @return string - */ - public function format() + public function format(): string { if ($this->noOutput) { return $this->redirectStandardOutTo(self::NO_FILE) . $this->redirectStandardErrorTo(self::NO_FILE); @@ -103,24 +72,24 @@ public function format() return $this->createOutput(); } - private function redirectStandardOutTo($filePath) + private function redirectStandardOutTo($filePath): string { return $this->redirectOutputTo('', $this->stdOutAppend, $filePath); } - private function redirectOutputTo($out, $isAppend, $filePath) + private function redirectOutputTo($out, $isAppend, $filePath): string { $operator = $isAppend ? '>>' : '>'; - return ' ' . $out . $operator . ' '. $filePath; + return ' ' . $out . $operator . ' ' . $filePath; } - private function redirectStandardErrorTo($filePath) + private function redirectStandardErrorTo($filePath): string { return $this->redirectOutputTo('2', $this->stdErrAppend, $filePath); } - private function createOutput() + private function createOutput(): string { $out = ''; diff --git a/src/Formatter/Time.php b/src/Formatter/Time.php index 36235cc..7202a46 100644 --- a/src/Formatter/Time.php +++ b/src/Formatter/Time.php @@ -23,40 +23,28 @@ */ class Time { - const FORMAT = '%-4s %-4s %-4s %-4s %-4s '; - const WILDCARD_TIME = '*'; + public const FORMAT = '%-4s %-4s %-4s %-4s %-4s '; + public const WILDCARD_TIME = '*'; - /** - * @var string - */ + /** @var string */ private $minute = self::WILDCARD_TIME; - /** - * @var string - */ + /** @var string */ private $hour = self::WILDCARD_TIME; - /** - * @var string - */ + /** @var string */ private $dayOfMonth = self::WILDCARD_TIME; - /** - * @var string - */ + /** @var string */ private $month = self::WILDCARD_TIME; - /** - * @var string - */ + /** @var string */ private $dayOfWeek = self::WILDCARD_TIME; /** * @param string $value 0-59 or a list or range - * - * @return $this */ - public function setMinute($value) + public function setMinute(string $value): self { $this->minute = $this->parse($value); @@ -65,10 +53,8 @@ public function setMinute($value) /** * @param string $value 0-23 or a list or range - * - * @return $this */ - public function setHour($value) + public function setHour(string $value): self { $this->hour = $this->parse($value); @@ -77,10 +63,8 @@ public function setHour($value) /** * @param string $value 0-31 or a list or range - * - * @return $this */ - public function setDayOfMonth($value) + public function setDayOfMonth(string $value): self { $this->dayOfMonth = $this->parse($value); @@ -89,10 +73,8 @@ public function setDayOfMonth($value) /** * @param string $value 0-12 or a list or range - * - * @return $this */ - public function setMonth($value) + public function setMonth(string $value): self { $this->month = $this->parse($value); @@ -101,32 +83,22 @@ public function setMonth($value) /** * @param string $value 0-7 (0 or 7 is Sun, or use names) or a list or range - * - * @return $this */ - public function setDayOfWeek($value) + public function setDayOfWeek(string $value): self { $this->dayOfWeek = $this->parse($value); return $this; } - /** - * @return string - */ - public function format() + public function format(): string { - return sprintf(self::FORMAT, $this->minute, $this->hour, $this->dayOfMonth, $this->month, $this->dayOfWeek); + return \sprintf(self::FORMAT, $this->minute, $this->hour, $this->dayOfMonth, $this->month, $this->dayOfWeek); } - /** - * @param string $value - * - * @return string - */ - private function parse($value) + private function parse(string $value): string { - if ('/' === $value[0]) { + if (0 === \strpos($value, '/')) { return self::WILDCARD_TIME . $value; } diff --git a/src/Updater/CommandCronManipulator.php b/src/Updater/CommandCronManipulator.php index 5a6a7b0..eba4206 100644 --- a/src/Updater/CommandCronManipulator.php +++ b/src/Updater/CommandCronManipulator.php @@ -4,26 +4,31 @@ class CommandCronManipulator implements CronManipulator { + /** @var ProcessRunner */ private $processRunner; + + /** @var FileSystem */ private $fileSystem; + + /** @var string */ private $cronCommand; - public function __construct(ProcessRunner $processRunner, FileSystem $fileSystem, $cronCommand = 'crontab') + public function __construct(ProcessRunner $processRunner, FileSystem $fileSystem, string $cronCommand = 'crontab') { $this->processRunner = $processRunner; $this->fileSystem = $fileSystem; $this->cronCommand = $cronCommand; } - public function replace($contents) + public function replace(string $contents): void { $filePath = $this->fileSystem->createTempFile('cron', $contents); - $this->processRunner->run($this->cronCommand . ' ' . $filePath); + $this->processRunner->run([$this->cronCommand, $filePath]); $this->fileSystem->removeFile($filePath); } - public function getContent() + public function getContent(): string { - return $this->processRunner->run($this->cronCommand . ' -l'); + return $this->processRunner->run([$this->cronCommand, '-l']); } } diff --git a/src/Updater/CronManipulator.php b/src/Updater/CronManipulator.php index 66bd472..c78091c 100644 --- a/src/Updater/CronManipulator.php +++ b/src/Updater/CronManipulator.php @@ -4,13 +4,7 @@ interface CronManipulator { - /* - * @param $contents string - */ - public function replace($contents); + public function replace(string $contents): void; - /** - * @return string - */ - public function getContent(); + public function getContent(): string; } diff --git a/src/Updater/CronUpdater.php b/src/Updater/CronUpdater.php index 1d76901..8638b0a 100644 --- a/src/Updater/CronUpdater.php +++ b/src/Updater/CronUpdater.php @@ -6,9 +6,10 @@ class CronUpdater { - const KEY_BEGIN = '# KEY %key%'; - const KEY_END = '# END'; + public const KEY_BEGIN = '# KEY %key%'; + public const KEY_END = '# END'; + /** @var CronManipulator */ private $cronManipulator; public function __construct(CronManipulator $cronManipulator) @@ -16,40 +17,29 @@ public function __construct(CronManipulator $cronManipulator) $this->cronManipulator = $cronManipulator; } - /** - * @return CronUpdater - */ - public static function createDefault() + public static function createDefault(): self { return new self(new CommandCronManipulator(new SymfonyProcessRunner, new StandardFileSystem)); } - /** - * @param Cron $cron - */ - public function replaceWith(Cron $cron) + public function replaceWith(Cron $cron): void { $this->cronManipulator->replace($cron->format()); } - /** - * @param Cron $cron - * @param $key - * - * @throws \Exception - */ - public function updateWith(Cron $cron, $key) + public function updateWith(Cron $cron, string $key): void { $this->cronManipulator->replace($this->updateContent($cron, $key)); } - private function updateContent(Cron $cron, $key) + private function updateContent(Cron $cron, string $key): string { $content = $this->cronManipulator->getContent(); $count = 0; $pattern = '/\r?\n' . $this->beginKey($key) . '.*?' . self::KEY_END . '/s'; - $replacedContent = preg_replace($pattern, $this->wrapInKey($cron, $key), $content, -1, $count); + $replacedContent = \preg_replace($pattern, $this->wrapInKey($cron, $key), $content, -1, $count); + if ($count > 0) { return $replacedContent; } @@ -57,18 +47,18 @@ private function updateContent(Cron $cron, $key) return $this->appendContent($cron, $key, $content); } - private function wrapInKey(Cron $cron, $key) + private function wrapInKey(Cron $cron, string $key): string { - return PHP_EOL . $this->beginKey($key) . PHP_EOL . trim($cron->format()) . PHP_EOL . self::KEY_END; + return \PHP_EOL . $this->beginKey($key) . \PHP_EOL . \trim($cron->format()) . \PHP_EOL . self::KEY_END; } - private function beginKey($key) + private function beginKey(string $key): string { - return str_replace('%key%', $key, self::KEY_BEGIN); + return \str_replace('%key%', $key, self::KEY_BEGIN); } - private function appendContent(Cron $cron, $key, $content) + private function appendContent(Cron $cron, string $key, string $content): string { - return $content . $this->wrapInKey($cron, $key) . PHP_EOL; + return $content . $this->wrapInKey($cron, $key) . \PHP_EOL; } } diff --git a/src/Updater/FileSystem.php b/src/Updater/FileSystem.php index f03423a..35f0667 100644 --- a/src/Updater/FileSystem.php +++ b/src/Updater/FileSystem.php @@ -4,16 +4,7 @@ interface FileSystem { - /** - * @param string $prefix - * @param string $content - * - * @return string - */ - public function createTempFile($prefix, $content); + public function createTempFile(string $prefix, string $content): string; - /** - * @param string $filePath - */ - public function removeFile($filePath); + public function removeFile(string $filePath): void; } diff --git a/src/Updater/ProcessRunner.php b/src/Updater/ProcessRunner.php index ee45be2..47eb1c5 100644 --- a/src/Updater/ProcessRunner.php +++ b/src/Updater/ProcessRunner.php @@ -4,10 +4,5 @@ interface ProcessRunner { - /** - * @param string $command - * - * @return string - */ - public function run($command); + public function run(array $command): string; } diff --git a/src/Updater/StandardFileSystem.php b/src/Updater/StandardFileSystem.php index 586a405..c8622b7 100644 --- a/src/Updater/StandardFileSystem.php +++ b/src/Updater/StandardFileSystem.php @@ -4,27 +4,21 @@ class StandardFileSystem implements FileSystem { - /** - * {@inheritdoc} - */ - public function createTempFile($prefix, $content) + public function createTempFile(string $prefix, string $content): string { $filePath = $this->createTempName($prefix); - file_put_contents($filePath, $content); + \file_put_contents($filePath, $content); return $filePath; } - private function createTempName($prefix) + private function createTempName(string $prefix): string { - return tempnam(sys_get_temp_dir(), $prefix); + return \tempnam(\sys_get_temp_dir(), $prefix); } - /** - * {@inheritdoc} - */ - public function removeFile($filePath) + public function removeFile(string $filePath): void { - unlink($filePath); + \unlink($filePath); } } diff --git a/src/Updater/SymfonyFileSystem.php b/src/Updater/SymfonyFileSystem.php index 288d075..7fd135e 100644 --- a/src/Updater/SymfonyFileSystem.php +++ b/src/Updater/SymfonyFileSystem.php @@ -6,9 +6,7 @@ class SymfonyFileSystem implements FileSystem { - /** - * @var FileSystemHelper; - */ + /** @var FileSystemHelper; */ private $filesystem; public function __construct() @@ -16,12 +14,8 @@ public function __construct() $this->filesystem = new FileSystemHelper(); } - /** - * {@inheritdoc} - * - * @throws \Symfony\Component\Filesystem\Exception\IOException If the file cannot be written to. - */ - public function createTempFile($prefix, $content) + /** @throws \Symfony\Component\Filesystem\Exception\IOException If the file cannot be written to. */ + public function createTempFile($prefix, $content): string { $filePath = $this->createTempName($prefix); $this->dumpToFile($filePath, $content); @@ -29,26 +23,22 @@ public function createTempFile($prefix, $content) return $filePath; } - private function dumpToFile($filePath, $content) + private function dumpToFile(string $filePath, string $content): void { - if (method_exists($this->filesystem, 'dumpFile')) { + if (\method_exists($this->filesystem, 'dumpFile')) { $this->filesystem->dumpFile($filePath, $content); } else { - file_put_contents($filePath, $content); + \file_put_contents($filePath, $content); } } - private function createTempName($prefix) + private function createTempName(string $prefix): string { - return tempnam(sys_get_temp_dir(), $prefix); + return \tempnam(\sys_get_temp_dir(), $prefix); } - /** - * {@inheritdoc} - * - * @throws \Symfony\Component\Filesystem\Exception\IOException When removal fails - */ - public function removeFile($filePath) + /** @throws \Symfony\Component\Filesystem\Exception\IOException When removal fails */ + public function removeFile(string $filePath): void { $this->filesystem->remove($filePath); } diff --git a/src/Updater/SymfonyProcessRunner.php b/src/Updater/SymfonyProcessRunner.php index 72365a7..d5e48ee 100644 --- a/src/Updater/SymfonyProcessRunner.php +++ b/src/Updater/SymfonyProcessRunner.php @@ -6,15 +6,12 @@ class SymfonyProcessRunner implements ProcessRunner { - /** - * {@inheritdoc} - */ - public function run($command) + public function run(array $command): string { $process = new Process($command); $process->run(); - if (!$process->isSuccessful()) { + if (false === $process->isSuccessful()) { throw new \RuntimeException($process->getErrorOutput()); } diff --git a/tests/Formatter/CronTest.php b/tests/Formatter/CronTest.php index bcd32dd..b01c018 100644 --- a/tests/Formatter/CronTest.php +++ b/tests/Formatter/CronTest.php @@ -1,12 +1,15 @@ cron = new Cron; } @@ -14,7 +17,7 @@ protected function setUp() /** * @test */ - public function shouldBuildConfiguration() + public function shouldBuildConfiguration(): void { $this->cron ->header() @@ -59,7 +62,7 @@ public function shouldBuildConfiguration() */5 * * * sun /bin/php command2 --env=prod > /dev/null 2> /dev/null EXP; - + $this->assertEquals($expected, $this->cron->format()); } } diff --git a/tests/Updater/CronUpdaterTest.php b/tests/Updater/CronUpdaterTest.php index 6e5eb3d..b8fc439 100644 --- a/tests/Updater/CronUpdaterTest.php +++ b/tests/Updater/CronUpdaterTest.php @@ -1,18 +1,20 @@ manipulatorStub = new CronManipulatorStub; + $this->manipulatorStub = new CronManipulatorStub(); $this->updater = new CronUpdater($this->manipulatorStub); } @@ -20,17 +22,17 @@ public function setUp() /** * @test */ - public function shouldReplaceContent() + public function shouldReplaceContent(): void { - $this->updater->replaceWith(new Cron); + $this->updater->replaceWith(new Cron()); - $this->assertEquals(PHP_EOL, $this->manipulatorStub->contents); + $this->assertEquals(\PHP_EOL, $this->manipulatorStub->contents); } /** * @test */ - public function shouldAppendKeyIfNotExist() + public function shouldAppendKeyIfNotExist(): void { $this->manipulatorStub->contents = <<header()->setPath('path')->end(); $cron->comment('new content'); $this->updater->updateWith($cron, 'key2'); @@ -65,7 +67,7 @@ public function shouldAppendKeyIfNotExist() /** * @test */ - public function shouldReplaceKeyIfExist() + public function shouldReplaceKeyIfExist(): void { $this->manipulatorStub->contents = <<comment('replace'); $this->updater->updateWith($cron, 'key2'); @@ -107,15 +109,16 @@ public function shouldReplaceKeyIfExist() class CronManipulatorStub implements CronManipulator { + /** @var string */ public $contents; - public function replace($contents) + public function replace(string $contents): void { $this->contents = $contents; } - public function getContent() + public function getContent(): string { return $this->contents; } -} \ No newline at end of file +}