Skip to content

Commit

Permalink
Refactor helpers.php
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Oct 27, 2024
1 parent cc39b3b commit a4b4783
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 62 deletions.
5 changes: 2 additions & 3 deletions recipe/provision/nodejs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Deployer;

use function Deployer\Support\escape_shell_argument;
use function Deployer\Support\starts_with;

set('node_version', '23.x');

Expand All @@ -16,9 +15,9 @@
}
$arch = run('uname -m');

if ($arch === 'arm' || starts_with($arch, 'armv7')) {
if ($arch === 'arm' || str_starts_with($arch, 'armv7')) {
$filename = 'fnm-arm32';
} elseif (starts_with($arch, 'aarch') || starts_with($arch, 'armv8')) {
} elseif (str_starts_with($arch, 'aarch') || str_starts_with($arch, 'armv8')) {
$filename = 'fnm-arm64';
} else {
$filename = 'fnm-linux';
Expand Down
4 changes: 0 additions & 4 deletions src/Command/MainCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Deployer\Command;

use Deployer\Configuration;
use Deployer\Deployer;
use Deployer\Exception\Exception;
use Deployer\Exception\GracefulShutdownException;
Expand All @@ -22,9 +21,6 @@
use Symfony\Component\Console\Input\InputOption as Option;
use Symfony\Component\Console\Output\OutputInterface as Output;

use function Deployer\Support\find_config_line;
use function Deployer\warning;

class MainCommand extends SelectCommand
{
use CustomOption;
Expand Down
2 changes: 0 additions & 2 deletions src/Documentation/DocGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use RecursiveRegexIterator;
use RegexIterator;

use function Deployer\Support\str_contains as str_contains;

class DocGen
{
/**
Expand Down
55 changes: 4 additions & 51 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@

namespace Deployer\Support;

/**
* Flatten array
*
* @param array $array
* @return array
*/
function array_flatten(array $array)
function array_flatten(array $array): array
{
$flatten = [];
array_walk_recursive($array, function ($value) use (&$flatten) {
Expand All @@ -31,13 +25,8 @@ function array_flatten(array $array)
* 1. scalar values are overridden
* 2. array values are extended uniquely if all keys are numeric
* 3. all other array values are merged
*
* @param array $original
* @param array $override
* @return array
* @see http://stackoverflow.com/a/36366886/6812729
*/
function array_merge_alternate(array $original, array $override)
function array_merge_alternate(array $original, array $override): array
{
foreach ($override as $key => $value) {
if (isset($original[$key])) {
Expand Down Expand Up @@ -65,26 +54,6 @@ function array_merge_alternate(array $original, array $override)
return $original;
}

/**
* Determines if the given string contains the given value.
*/
function str_contains(string $haystack, string $needle): bool
{
return strpos($haystack, $needle) !== false;
}

/**
* Checks if string stars with given prefix.
*/
function starts_with(string $string, string $prefix): bool
{
$len = strlen($prefix);
return (substr($string, 0, $len) === $prefix);
}

/**
* This function used for create environment string.
*/
function env_stringify(array $array): string
{
return implode(' ', array_map(
Expand All @@ -96,11 +65,7 @@ function ($key, $value) {
));
}

/**
* Check if var is closure.
* @param mixed $var
*/
function is_closure($var): bool
function is_closure(mixed $var): bool
{
return is_object($var) && ($var instanceof \Closure);
}
Expand Down Expand Up @@ -131,7 +96,7 @@ function normalize_line_endings(string $string): string
*/
function parse_home_dir(string $path): string
{
if ('~' === $path || 0 === strpos($path, '~/')) {
if ('~' === $path || str_starts_with($path, '~/')) {
if (isset($_SERVER['HOME'])) {
$home = $_SERVER['HOME'];
} elseif (isset($_SERVER['HOMEDRIVE'], $_SERVER['HOMEPATH'])) {
Expand All @@ -156,18 +121,6 @@ function find_line_number(string $source, string $string): int
return 1;
}

function find_config_line(string $source, string $name): \Generator
{
foreach (explode(PHP_EOL, $source) as $n => $line) {
if (preg_match("/\(['\"]{$name}['\"]/", $line)) {
yield [$n + 1, $line];
}
if (preg_match("/\s{$name}:/", $line)) {
yield [$n + 1, $line];
}
}
}

function colorize_host(string $alias): string
{
if (defined('NO_ANSI')) {
Expand Down
2 changes: 0 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@

use function Deployer\Support\array_merge_alternate;
use function Deployer\Support\env_stringify;
use function Deployer\Support\escape_shell_argument;
use function Deployer\Support\is_closure;
use function Deployer\Support\str_contains;

/**
* Defines a host or hosts.
Expand Down

0 comments on commit a4b4783

Please sign in to comment.