Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
duncan3dc authored Oct 30, 2024
2 parents 24e6ecd + b06b9b0 commit e888183
Show file tree
Hide file tree
Showing 52 changed files with 507 additions and 138 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/tests export-ignore
/.scrutinizer.yml export-ignore
/.styleci.yml export-ignore
/phpunit.xml export-ignore
/phpunit.xml.dist export-ignore
33 changes: 14 additions & 19 deletions .github/workflows/buildcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,15 @@ jobs:
fail-fast: false
matrix:
php:
- 7.3
- 7.4
- 8.0-rc
- "7.3"
- "7.4"
- "8.0"
- "8.1"
- "8.2"
- "8.3"
composer:
- ""
- "--prefer-lowest"
include:
- php: 8.0-rc
composer: "--ignore-platform-reqs"
exclude:
- php: 8.0-rc
composer: ""
- php: 8.0-rc
composer: "--prefer-lowest"

steps:
- uses: actions/checkout@v1
Expand All @@ -42,18 +37,18 @@ jobs:
- name: PHPUnit
run: docker exec ci vendor/bin/phpunit

- name: Coding Standards
run: docker exec ci vendor/bin/phpcs

- name: Composer Validate
run: docker exec ci composer validate --strict

windows:
runs-on: windows-2019
runs-on: windows-latest
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
steps:
- uses: actions/checkout@master
- uses: nanasess/setup-php@master
- uses: nanasess/composer-installer-action@master
- name: Install dependencies
run: composer update
- name: Run test suite
run: vendor/bin/phpunit
- uses: actions/checkout@v4
- uses: nanasess/setup-php@v4
- run: composer update
- run: vendor/bin/phpunit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/composer.lock
/phpunit.xml
/vendor
3 changes: 0 additions & 3 deletions .styleci.yml

This file was deleted.

29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@ Changelog

--------

## 3.8.2 - 2022-01-23

### Fixed

* Avoid passing null to strlen(). [#186](https://github.com/thephpleague/climate/issues/186)
* [Support] Added compatibility for psr/log 2 & 3. [#191](https://github.com/thephpleague/climate/issues/191)

--------

## 3.8.1 - 2022-01-23

### Fixed

* [Linux] Avoid type issue when checking if \STDOUT is defined. [#185](https://github.com/thephpleague/climate/issues/185)

--------

## 3.8.0 - 2022-01-22

### Changed

* [Support] Added support for PHP 8.1.

### Fixed

* [Linux] Added a workaround for executed some parts of the code in a non-cli context. [#175](https://github.com/thephpleague/climate/pull/175)

--------

## 3.7.0 - 2021-01-10

### Changed
Expand Down
14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
}
],
"require": {
"psr/log": "^1.0",
"php": "^7.3 || ^8.0",
"seld/cli-prompt": "^1.0"
"psr/log": "^1.0 || ^2.0 || ^3.0",
"seld/cli-prompt": "^1.0",
"php": "^7.3 || ^8.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5.0",
"mockery/mockery": "^1.4.2",
"mikey179/vfsstream": "^1.4"
"mockery/mockery": "^1.6.12",
"mikey179/vfsstream": "^1.6.12",
"squizlabs/php_codesniffer": "^3.10",
"phpunit/phpunit": "^9.5.10"
},
"suggest": {
"ext-mbstring": "If ext-mbstring is not available you MUST install symfony/polyfill-mbstring"
Expand All @@ -42,6 +43,7 @@
"scripts": {
"test": [
"vendor/bin/phpunit",
"vendor/bin/phpcs",
"@composer validate --strict"
]
}
Expand Down
26 changes: 26 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<ruleset name="CLImate">

<file>src</file>
<file>tests</file>

<rule ref="PSR1" />
<rule ref="PSR2" />
<rule ref="PSR12" />

<rule ref="Generic.Files.LineLength">
<severity>0</severity>
</rule>
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<severity>0</severity>
</rule>
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
<severity>0</severity>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
<properties>
<property name="ignoreBlankLines" value="false" />
</properties>
</rule>

</ruleset>
17 changes: 0 additions & 17 deletions phpunit.xml

This file was deleted.

10 changes: 10 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<phpunit bootstrap="tests/bootstrap.php">
<testsuite name="CLImate">
<directory suffix="Test.php">tests</directory>
</testsuite>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
6 changes: 5 additions & 1 deletion src/Argument/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace League\CLImate\Argument;

use League\CLImate\Exceptions\UnexpectedValueException;

use function is_array;

class Argument
Expand Down Expand Up @@ -262,8 +263,11 @@ public function noValue()
*/
protected function setNoValue($noValue)
{
$this->setCastTo('bool');
$this->noValue = (bool) $noValue;

if ($this->noValue === true) {
$this->setCastTo('bool');
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/Argument/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,24 @@ public function trailingArray()
{
return $this->parser->trailingArray();
}

/**
* Returns the list of unknown prefixed arguments and their suggestions.
*
* @return array The list of unknown prefixed arguments and their suggestions.
*/
public function getUnknowPrefixedArgumentsAndSuggestions()
{
return $this->parser->getUnknowPrefixedArgumentsAndSuggestions();
}

/**
* Sets the minimum similarity percentage for finding suggestions.
*
* @param float $percentage The minimum similarity percentage to set.
*/
public function setMinimumSimilarityPercentage(float $percentage)
{
$this->parser->setMinimumSimilarityPercentage($percentage);
}
}
115 changes: 115 additions & 0 deletions src/Argument/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ class Parser

protected $trailingArray;

/**
* List of unknown arguments and best argument suggestion.
*
* The key corresponds to the unknown argument and the value to the
* argument suggestion, if any.
*
* @var array
*/
protected $unknowPrefixedArguments = [];

/**
* Minimum similarity percentage to detect similar arguments.
*
* @var float
*/
protected $minimumSimilarityPercentage = 0.6;

public function __construct()
{
$this->summary = new Summary();
Expand Down Expand Up @@ -61,6 +78,10 @@ public function parse(array $argv = null)

$unParsedArguments = $this->prefixedArguments($cliArguments);

// Searches for unknown prefixed arguments and finds a suggestion
// within the list of valid arguments.
$this->unknowPrefixedArguments($unParsedArguments);

$this->nonPrefixedArguments($unParsedArguments);

// After parsing find out which arguments were required but not
Expand Down Expand Up @@ -306,4 +327,98 @@ protected function getCommandAndArguments(array $argv = null)

return compact('arguments', 'command');
}

/**
* Processes unknown prefixed arguments and sets suggestions if no matching
* prefix is found.
*
* @param array $unParsedArguments The array of unparsed arguments to
* process.
*/
protected function unknowPrefixedArguments(array $unParsedArguments)
{
foreach ($unParsedArguments as $arg) {
$unknowArgumentName = $this->getUnknowArgumentName($arg);
if (!$this->findPrefixedArgument($unknowArgumentName)) {
if (is_null($unknowArgumentName)) {
continue;
}
$suggestion = $this->findSuggestionsForUnknowPrefixedArguments(
$unknowArgumentName,
$this->filter->withPrefix()
);
$this->setSuggestion($unknowArgumentName, $suggestion);
}
}
}

/**
* Sets the suggestion for an unknown argument name.
*
* @param string $unknowArgName The name of the unknown argument.
* @param string $suggestion The suggestion for the unknown argument.
*/
protected function setSuggestion(string $unknowArgName, string $suggestion)
{
$this->unknowPrefixedArguments[$unknowArgName] = $suggestion;
}

/**
* Extracts the unknown argument name from a given argument string.
*
* @param string $arg The argument string to process.
* @return string|null The extracted unknown argument name or null if not
* found.
*/
protected function getUnknowArgumentName(string $arg)
{
if (preg_match('/^[-]{1,2}([^-]+?)(?:=|$)/', $arg, $matches)) {
return $matches[1];
}
return null;
}

/**
* Finds the most similar known argument for an unknown prefixed argument.
*
* @param string $argName The name of the unknown argument to find
* suggestions for.
* @param array $argList The list of known arguments to compare against.
* @return string The most similar known argument name.
*/
protected function findSuggestionsForUnknowPrefixedArguments(
string $argName,
array $argList
) {
$mostSimilar = '';
$greatestSimilarity = $this->minimumSimilarityPercentage * 100;
foreach ($argList as $arg) {
similar_text($argName, $arg->name(), $percent);
if ($percent > $greatestSimilarity) {
$greatestSimilarity = $percent;
$mostSimilar = $arg->name();
}
}
return $mostSimilar;
}

/**
* Returns the list of unknown prefixed arguments and their suggestions.
*
* @return array The list of unknown prefixed arguments and their suggestions.
*/
public function getUnknowPrefixedArgumentsAndSuggestions()
{
return $this->unknowPrefixedArguments;
}

/**
* Sets the minimum similarity percentage for finding suggestions.
*
* @param float $percentage The minimum similarity percentage to set.
*/
public function setMinimumSimilarityPercentage(float $percentage)
{
$this->minimumSimilarityPercentage = $percentage;
}
}
3 changes: 2 additions & 1 deletion src/CLImate.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,11 @@ public function to($writer)
* Output the program's usage statement
*
* @param array $argv
* @return void
*/
public function usage(array $argv = null)
{
return $this->arguments->usage($this, $argv);
$this->arguments->usage($this, $argv);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Decorator/Component/BackgroundColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BackgroundColor extends Color
*
* @const integer ADD
*/
const ADD = 10;
public const ADD = 10;

/**
* Get the code for the requested color
Expand Down
Loading

0 comments on commit e888183

Please sign in to comment.