Skip to content

Commit

Permalink
Code style fixes (#1762)
Browse files Browse the repository at this point in the history
* Update missing argument in PHPDoc.

* Splits the IF-statement/workflow(s) for readability

* Replace double quotes to single quotes.

* Replaced alias functions by original functions.

* Define return types.

* Type casting can be used

* Non-optimal regular expression, \d is a subset of \w.
  • Loading branch information
cafferata authored and antonmedv committed Nov 11, 2018
1 parent 231d7dc commit 2ad907e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 35 deletions.
26 changes: 13 additions & 13 deletions bin/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ $app->register('update')->setCode(function () use ($io) {

$item = new Item();

$message = $io->ask("What is " . strtolower($type) . '?');
$message = $io->ask('What is ' . strtolower($type) . '?');
$message = ucfirst($message);
$message = trim(rtrim($message, '.'));

Expand All @@ -45,28 +45,28 @@ $app->register('update')->setCode(function () use ($io) {
$url = $io->ask('Closed issues/pulls url', $noop);
if ($url === $noop) {
break;
}

if (preg_match('#^https\://github\.com/deployphp/deployer/(issues|pull)/(\d+)$#', $url, $m)) {
$ref = (int)$m[2];
$item->addReference($ref);
$changelog->addReferences($ref, $url);
} else {
if (preg_match('#^https\://github\.com/deployphp/deployer/(issues|pull)/(\d+)$#', $url, $m)) {
$ref = (int)$m[2];
$item->addReference($ref);
$changelog->addReferences($ref, $url);
} else {
$io->warning('Url should be link to GitHub issue or pull page of deployphp/deployer repo.');
}
$io->warning('Url should be link to GitHub issue or pull page of deployphp/deployer repo.');
}
} while (true);

$changelog->findMaster()->{"add$type"}($item);

file_put_contents(CHANGELOG, "$changelog");
file_put_contents(CHANGELOG, (string)$changelog);

$io->success(CHANGELOG . ' updated');
});

$app->register('fix')->setCode(function () use ($io) {
$parser = new Parser(file_get_contents(CHANGELOG), false);
$changelog = $parser->parse();
file_put_contents(CHANGELOG, "$changelog");
file_put_contents(CHANGELOG, (string)$changelog);
});

$app->register('release')->setCode(function () use ($io) {
Expand All @@ -77,16 +77,16 @@ $app->register('release')->setCode(function () use ($io) {

do {
$v = $io->ask("What version to release? (latest {$latest->getVersion()})");
if (!preg_match('/^\d+\.\d+\.\d+(-[\d\w\.]+)?$/', $v)) {
$io->warning("Version number must follow semantic versioning.");
if (!preg_match('/^\d+\.\d+\.\d+(-[\w\.]+)?$/', $v)) {
$io->warning('Version number must follow semantic versioning.');
continue;
}
break;
} while (true);

$changelog->findMaster()->setVersion('v' . $v);

file_put_contents(CHANGELOG, "$changelog");
file_put_contents(CHANGELOG, (string)$changelog);
$io->success(CHANGELOG . " updated to v$v");
});

Expand Down
2 changes: 1 addition & 1 deletion src/Support/Changelog/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __toString()
{
sort($this->references, SORT_NUMERIC);

$references = join('', array_map(function ($ref) {
$references = implode('', array_map(function ($ref) {
return " [#$ref]";
}, $this->references));

Expand Down
2 changes: 1 addition & 1 deletion src/Support/Changelog/ParseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class ParseException extends \Exception
{
public function __construct(string $message = "", $code = "")
public function __construct(string $message = '', $code = '')
{
parent::__construct("$message\n\n{$code}\n\n");
}
Expand Down
24 changes: 12 additions & 12 deletions src/Support/Changelog/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(string $changelog, bool $strict = true)
private function current(): string
{
if (count($this->tokens) === 0) {
return "";
return '';
}
return $this->tokens[0];
}
Expand All @@ -64,8 +64,8 @@ private function next(): string
private function acceptEmptyLine()
{
if ($this->strict) {
if ("" !== $this->next()) {
throw $this->error("Expected an empty line");
if ('' !== $this->next()) {
throw $this->error('Expected an empty line');
}
} else {
while (preg_match('/^\s*$/', $this->current()) && count($this->tokens) > 0) {
Expand All @@ -78,13 +78,13 @@ private function acceptEof()
{
if (count($this->tokens) !== 0) {
$this->next();
throw $this->error("Expected EOF");
throw $this->error('Expected EOF');
}
}

private function matchVersion($line, &$m = null)
{
return preg_match('/^\#\# \s ( v\d+\.\d+\.\d+(-[\d\w\.]+)? | master )$/x', $line, $m);
return preg_match('/^\#\# \s ( v\d+\.\d+\.\d+(-[\w\.]+)? | master )$/x', $line, $m);
}

private function error($message): ParseException
Expand All @@ -96,7 +96,7 @@ private function error($message): ParseException
$this->next();
}

return new ParseException($message, join("\n", $this->span));
return new ParseException($message, implode("\n", $this->span));
}

public function parse(): Changelog
Expand Down Expand Up @@ -128,7 +128,7 @@ private function parseTitle(): Changelog
return $c;
}

throw $this->error("Expected title");
throw $this->error('Expected title');
}

private function parseVersion(): Version
Expand All @@ -139,7 +139,7 @@ private function parseVersion(): Version

$compareLink = $this->next();
if (!preg_match('/^\[/', $compareLink)) {
throw $this->error("Expected link to compare page with previous version");
throw $this->error('Expected link to compare page with previous version');
}

$prev = 'v\d+\.\d+\.\d+(-[\d\w\.]+)?';
Expand All @@ -151,7 +151,7 @@ private function parseVersion(): Version
if (preg_match($regexp, $compareLink, $m)) {
$version->setPrevious($m[1]);
} else {
throw $this->error("Error in compare link syntax");
throw $this->error('Error in compare link syntax');
}

$this->acceptEmptyLine();
Expand Down Expand Up @@ -179,10 +179,10 @@ private function parseVersion(): Version
return $version;
}

throw $this->error("Expected version");
throw $this->error('Expected version');
}

private function parseItems()
private function parseItems(): array
{
$items = [];
while (preg_match('/^\- (.+) $/x', $this->current(), $m)) {
Expand All @@ -204,7 +204,7 @@ private function parseItems()
return $items;
}

private function parseReferences()
private function parseReferences(): array
{
$refs = [];
while (preg_match('/^\[/', $this->current())) {
Expand Down
16 changes: 8 additions & 8 deletions src/Support/Changelog/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@ public function __toString()
return "- $item";
};

$added = "";
$changed = "";
$fixed = "";
$removed = "";
$added = '';
$changed = '';
$fixed = '';
$removed = '';
if (!empty($this->added)) {
$added = join("\n", array_map($f, $this->added));
$added = implode("\n", array_map($f, $this->added));
$added = "### Added\n$added\n\n";
}
if (!empty($this->changed)) {
$changed = join("\n", array_map($f, $this->changed));
$changed = implode("\n", array_map($f, $this->changed));
$changed = "### Changed\n$changed\n\n";
}
if (!empty($this->fixed)) {
$fixed = join("\n", array_map($f, $this->fixed));
$fixed = implode("\n", array_map($f, $this->fixed));
$fixed = "### Fixed\n$fixed\n\n";
}
if (!empty($this->removed)) {
$removed = join("\n", array_map($f, $this->removed));
$removed = implode("\n", array_map($f, $this->removed));
$removed = "### Removed\n$removed\n\n";
}

Expand Down
4 changes: 4 additions & 0 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ function str_contains(string $haystack, string $needle)
* Take array of key/value and create string of it.
*
* This function used for create environment string.
*
* @param array $array
*
* @return string
*/
function array_to_string(array $array): string
{
Expand Down

0 comments on commit 2ad907e

Please sign in to comment.