Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'release/2.0.2'

  • Loading branch information...
commit 6f1fe958de26462b7c2b937b2bfc0e6c615e950f 2 parents ed6e002 + e6f041f
Stuart Herbert authored
4 build.properties
View
@@ -1,13 +1,13 @@
project.name=CommandLineLib
project.majorVersion=2
project.minorVersion=0
-project.patchLevel=1
+project.patchLevel=2
project.snapshot=false
checkstyle.standard=Zend
component.type=php-library
-component.version=10
+component.version=11
project.channel=pear.phix-project.org
pear.local=/var/www/${project.channel}
103 build.xml
View
@@ -63,7 +63,7 @@
<property file="${project.distdir.lastBuilt}"/>
</then>
<else>
- <property name="project.lastBuiltTarfile" value="false"/>
+ <property name="project.lastBuiltTarfile" value="false"/>
</else>
</if>
@@ -114,7 +114,7 @@
<taskdef name="phingcallifexists" classname="Phix_Project.ComponentManager.Phing.PhingCallIfExistsTask" />
<import file="build.local.xml"/>
-
+
<!-- Tell the user what this build file supports -->
<target name="help">
<echo message="${project.name} ${project.version}: build.xml targets:" />
@@ -139,6 +139,8 @@
<echo message=" Run code quality tests for PHP_CodeBrowser" />
<echo message=" phpcpd" />
<echo message=" Check for cut and paste problems" />
+ <echo message=" phploc" />
+ <echo message=" Calculate the size of your PHP project" />
<echo message=" phpdoc" />
<echo message=" Create the PHP docs from source code" />
<echo message="" />
@@ -205,7 +207,7 @@
<delete dir="${project.review.codecoveragedir}" />
<mkdir dir="${project.review.codecoveragedir}" />
<mkdir dir="${project.review.logsdir}" />
- <exec command="phpunit --configuration=phpunit.xml ${project.src.testunitdir}" checkreturn="true" logoutput="true"/>
+ <exec command="phpunit" checkreturn="true" logoutput="true"/>
<echo/>
<echo>The code coverage report is in file://${project.review.codecoveragedir}</echo>
<echo/>
@@ -217,7 +219,7 @@
</target>
<!-- Run the code review quality tests -->
- <target name="code-review" depends="run-unittests, code-browser, phpcpd, pdepend">
+ <target name="code-review" depends="run-unittests, code-browser, phpcpd, pdepend, phploc">
<phingcallifexists target="local.code-review"/>
</target>
@@ -263,6 +265,12 @@
<exec command="phpcb --log ${project.review.logsdir} --source ${project.src.phpdir} --output ${project.review.codebrowserdir}" logoutput="true" />
</target>
+ <!-- Work out the size of the project -->
+ <target name="phploc">
+ <mkdir dir="${project.review.logsdir}" />
+ <exec command="phploc --log-xml ${project.review.logsdir}/phploc.xml --log-csv ${project.review.logsdir}/phploc.csv ${project.src.phpdir}" logoutput="true" />
+ </target>
+
<!-- Populate vendor with the dependencies for this component -->
<target name="build-vendor" depends="pear-package,setup-vendor">
<echo>Populating vendor/ with dependencies</echo>
@@ -298,13 +306,55 @@
<echo>Building release directory</echo>
<delete dir="${project.builddir}" />
<mkdir dir="${project.pkgdir}" />
+ <if>
+ <available file="${project.src.bindir}"/>
+ <then>
+ <copy todir="${project.pkgdir}">
+ <fileset refid="binfiles"/>
+ </copy>
+ </then>
+ </if>
+ <if>
+ <available file="${project.src.datadir}"/>
+ <then>
+ <copy todir="${project.pkgdir}">
+ <fileset refid="datafiles"/>
+ </copy>
+ </then>
+ </if>
+ <if>
+ <available file="${project.src.docdir}"/>
+ <then>
+ <copy todir="${project.pkgdir}">
+ <fileset refid="docfiles"/>
+ </copy>
+ </then>
+ </if>
+ <if>
+ <available file="${project.src.phpdir}"/>
+ <then>
+ <copy todir="${project.pkgdir}">
+ <fileset refid="phpfiles"/>
+ </copy>
+ </then>
+ </if>
+ <if>
+ <available file="${project.src.testunitdir}"/>
+ <then>
+ <copy todir="${project.pkgdir}">
+ <fileset refid="testfiles"/>
+ </copy>
+ </then>
+ </if>
+ <if>
+ <available file="${project.src.wwwdir}"/>
+ <then>
+ <copy todir="${project.pkgdir}">
+ <fileset refid="wwwfiles"/>
+ </copy>
+ </then>
+ </if>
<copy todir="${project.pkgdir}">
- <fileset refid="binfiles"/>
- <fileset refid="datafiles"/>
- <fileset refid="phpfiles"/>
- <fileset refid="testfiles"/>
- <fileset refid="wwwfiles"/>
- <fileset refid="docfiles"/>
<fileset refid="topleveldocfiles"/>
</copy>
<copy todir="${project.builddir}">
@@ -325,8 +375,11 @@
</fileset>
</tar>
- <!-- write a message to say which file we built last -->
+ <!-- remember the tarball we have just build -->
+ <property name="project.lastBuiltTarfile" value="${project.tarfile}" override="true"/>
<echo file="${project.distdir.lastBuilt}" append="false">project.lastBuiltTarfile=${project.tarfile}</echo>
+
+ <!-- write a message to say which file we built last -->
<echo>Your PEAR package is in ${project.tarfile}</echo>
<phingcallifexists target="local.pear-package"/>
</target>
@@ -338,18 +391,28 @@
<contains string="${project.lastBuiltTarfile}" substring="${project.name}"/>
</not>
<then>
- <echo>Please run 'phing pear-package' first, then try again.</echo>
+ <fail message="Please run 'phing pear-package' first, then try again."/>
+ </then>
+ </if>
+
+ <if>
+ <not>
+ <available file="${project.vendordir"/>
+ </not>
+ <then>
+ <phingcall target="build-vendor" />
+ </then>
+ </if>
+
+ <if>
+ <available file="${project.lastBuiltTarfile}"/>
+ <then>
+ <exec command="pear -c ${project.tmpdir}/pear-config install --alldeps -f ${project.lastBuiltTarfile}" logoutput="true" checkreturn="true"/>
+ <phingcallifexists target="local.install-vendor"/>
</then>
- <elseif>
- <available file="${project.lastBuiltTarfile}"/>
- <then>
- <exec command="pear -c ${project.tmpdir}/pear-config install --alldeps -f ${project.lastBuiltTarfile}" logoutput="true" checkreturn="true"/>
- <phingcallifexists target="local.install-vendor"/>
- </then>
- </elseif>
<else>
<echo>Cannot find PEAR package file ${project.lastBuiltTarfile}</echo>
- <echo>Run 'phing pear-package' to create a new PEAR package, then try again</echo>
+ <fail message="Run 'phing pear-package' to create a new PEAR package, then try again."/>
</else>
</if>
</target>
6 package.xml
View
@@ -46,6 +46,12 @@ ${contents}
<max>3.999.9999</max>
</package>
<package>
+ <name>ContractLib</name>
+ <channel>pear.phix-project.org</channel>
+ <min>2.0.0</min>
+ <max>2.999.9999</max>
+ </package>
+ <package>
<name>ValidationLib</name>
<channel>pear.phix-project.org</channel>
<min>3.0.0</min>
0  phpunit.xml → phpunit.xml.dist
View
File renamed without changes
68 src/README.txt
View
@@ -0,0 +1,68 @@
+Your src/ folder
+================
+
+This src/ folder is where you put all of your code for release. There's
+a folder for each type of file that the PEAR Installer supports. You can
+find out more about these file types online at:
+
+http://blog.stuartherbert.com/php/2011/04/04/explaining-file-roles/
+
+ * bin/
+
+ If you're creating any command-line tools, this is where you'd put
+ them. Files in here get installed into /usr/bin on Linux et al.
+
+ There is more information available here: http://blog.stuartherbert.com/php/2011/04/06/php-components-shipping-a-command-line-program/
+
+ You can find an example here: https://github.com/stuartherbert/phix/tree/master/src/bin
+
+ * data/
+
+ If you have any data files (any files that aren't PHP code, and which
+ don't belong in the www/ folder), this is the folder to put them in.
+
+ There is more information available here: http://blog.stuartherbert.com/php/2011/04/11/php-components-shipping-data-files-with-your-components/
+
+ You can find an example here: https://github.com/stuartherbert/ComponentManagerPhpLibrary/tree/master/src/data
+
+ * php/
+
+ This is where your component's PHP code belongs. Everything that goes
+ into this folder must be PSR0-compliant, so that it works with the
+ supplied autoloader.
+
+ There is more information available here: http://blog.stuartherbert.com/php/2011/04/05/php-components-shipping-reusable-php-code/
+
+ You can find an example here: https://github.com/stuartherbert/ContractLib/tree/master/src/php
+
+ * tests/functional-tests/
+
+ Right now, this folder is just a placeholder for future functionality.
+ You're welcome to make use of it yourself.
+
+ * tests/integration-tests/
+
+ Right now, this folder is just a placeholder for future functionality.
+ You're welcome to make use of it yourself.
+
+ * tests/unit-tests/
+
+ This is where all of your PHPUnit tests go.
+
+ It needs to contain _exactly_ the same folder structure as the src/php/
+ folder. For each of your PHP classes in src/php/, there should be a
+ corresponding test file in test/unit-tests.
+
+ There is more information available here: http://blog.stuartherbert.com/php/2011/08/15/php-components-shipping-unit-tests-with-your-component/
+
+ You can find an example here: https://github.com/stuartherbert/ContractLib/tree/master/test/unit-tests
+
+ * www/
+
+ This folder is for any files that should be published in a web server's
+ DocRoot folder.
+
+ It's quite unusual for components to put anything in this folder, but
+ it is there just in case.
+
+ There is more information available here: http://blog.stuartherbert.com/php/2011/08/16/php-components-shipping-web-pages-with-your-components/
192 src/php/Phix_Project/CommandLineLib/CommandLineParser.php
View
@@ -46,16 +46,48 @@
namespace Phix_Project\CommandLineLib;
+use Phix_Project\ContractLib\Contract;
+
+/**
+ * Main entry point for parsing a command line, looking for any expected
+ * switches and their (possibly optional) arguments
+ */
class CommandLineParser
{
+ /**
+ * Parse an array of command-line arguments, looking for command-
+ * line switches
+ *
+ * @param array $args
+ * The array of command-line arguments (normally $argv)
+ * @param int $argIndex
+ * The current index inside $args to search from
+ * @param DefinedSwitches $expectedOptions
+ * The list of command-line switches that we support
+ * @return array(ParsedSwitches, int)
+ * The set of parsed command line switches, plus the new value
+ * for $argIndex
+ */
public function parseSwitches($args, $argIndex, DefinedSwitches $expectedOptions)
{
+ // catch programming errors
+ Contract::Preconditions(function() use ($args, $argIndex, $expectedOptions)
+ {
+ Contract::RequiresValue($args, is_array($args), '$args must be array');
+ Contract::RequiresValue($args, count($args) > 0, '$args cannot be an empty array');
+
+ Contract::RequiresValue($argIndex, is_integer($argIndex), '$argIndex must be an integer');
+ Contract::RequiresValue($argIndex, count($args) >= $argIndex, '$argIndex cannot be more than +1 beyond the end of $args');
+
+ Contract::RequiresValue($expectedOptions, count($expectedOptions->getSwitches()) > 0, '$expectedOptions must have some switches defined');
+ });
+
// create our return values
- $ParsedSwitches = new ParsedSwitches($expectedOptions);
+ $parsedSwitches = new ParsedSwitches($expectedOptions);
$argCount = count($args);
// var_dump($args);
-
+
// let's work through the args from left to right
$done = false;
while ($argIndex < $argCount && !$done)
@@ -68,7 +100,7 @@ public function parseSwitches($args, $argIndex, DefinedSwitches $expectedOptions
// skip over it
$argIndex++;
$done = true;
- }
+ }
else if ($args[$argIndex]{0} !== '-')
{
// var_dump('Not a switch');
@@ -82,34 +114,83 @@ public function parseSwitches($args, $argIndex, DefinedSwitches $expectedOptions
// var_dump('Parsing short switch');
// var_dump('$argIndex is: ' . $argIndex);
// it is a short switch
- $argIndex = $this->parseShortSwitch($args, $argIndex, $ParsedSwitches, $expectedOptions);
+ $argIndex = $this->parseShortSwitch($args, $argIndex, $parsedSwitches, $expectedOptions);
// var_dump('$argIndex is now: ' . $argIndex);
}
else
{
// var_dump('Parsing long switch');
// it is a long switch
- $argIndex = $this->parseLongSwitch($args, $argIndex, $ParsedSwitches, $expectedOptions);
+ $argIndex = $this->parseLongSwitch($args, $argIndex, $parsedSwitches, $expectedOptions);
}
}
// now, we need to merge in the default values for any
// arguments that have not been specified by the user
$defaultValues = $expectedOptions->getDefaultValues();
-
+
foreach ($defaultValues as $name => $value)
{
if ($value !== null && $expectedOptions->getSwitchByName($name)->testHasArgument())
{
- $ParsedSwitches->addDefaultValue($expectedOptions, $name, $value);
+ $parsedSwitches->addSwitchWithDefaultValueIfUnseen($expectedOptions, $name, $value);
}
}
-
- return array($ParsedSwitches, $argIndex);
+
+ return array($parsedSwitches, $argIndex);
}
- protected function parseShortSwitch($args, $argIndex, ParsedSwitches $ParsedSwitches, DefinedSwitches $expectedOptions)
+ /**
+ * Take a short switch, and see if it is one that we understand
+ *
+ * This parser supports short switches of the form:
+ *
+ * * -x
+ * where 'x' is a single switch
+ * * -xfred
+ * where 'x' is a single switch, and 'fred' is its argument
+ * * -x fred
+ * where 'x' is a single switch, and 'fred' is its argument
+ * * -xyz
+ * where 'x', 'y' and 'z' are all short switches
+ * * -xyz fred
+ * where 'x', 'y' and 'z' are all short switches, and 'fred'
+ * is the argument to switch 'z'
+ *
+ * These are all of the common short switch types traditionally
+ * supported by UNIX-like systems
+ *
+ * All successfully parsed short switches are added to the
+ * $parsedSwitches object.
+ *
+ * Any unexpected switches, or if there are any switches which are
+ * missing their required argument, will trigger an exception
+ *
+ * @param array $args
+ * The array of command-line arguments (normally $argv)
+ * @param int $argIndex
+ * The current index inside $args to search from
+ * @param ParsedSwitches $parsedSwitches
+ * The list of switches we have already parsed
+ * @param DefinedSwitches $expectedOptions
+ * The list of command-line switches that we support
+ * @return int
+ * The new value for $argIndex
+ */
+ protected function parseShortSwitch($args, $argIndex, ParsedSwitches $parsedSwitches, DefinedSwitches $expectedOptions)
{
+ // catch programming errors
+ Contract::Preconditions(function() use ($args, $argIndex, $expectedOptions)
+ {
+ Contract::RequiresValue($args, is_array($args), '$args must be array');
+ Contract::RequiresValue($args, count($args) > 0, '$args cannot be an empty array');
+
+ Contract::RequiresValue($argIndex, is_integer($argIndex), '$argIndex must be an integer');
+ Contract::RequiresValue($argIndex, count($args) > $argIndex, '$argIndex cannot be beyond the end of $args');
+
+ Contract::RequiresValue($expectedOptions, count($expectedOptions->getSwitches()) > 0, '$expectedOptions must have some switches defined');
+ });
+
// $args[$argIndex] contains one or more short switches,
// which we expect to be defined in $expectedOptions
@@ -131,7 +212,7 @@ protected function parseShortSwitch($args, $argIndex, ParsedSwitches $ParsedSwit
// should it have an argument?
if ($switch->testHasArgument())
- {
+ {
// yes, but it may be optional
// are we the first switch in this string?
if ($j == 1)
@@ -151,7 +232,7 @@ protected function parseShortSwitch($args, $argIndex, ParsedSwitches $ParsedSwit
}
else
{
- // are we at the end of the list of switches?
+ // are we at the end of the list of switches?
if ($j != $switchStringLength - 1)
{
// no, we are not
@@ -164,7 +245,7 @@ protected function parseShortSwitch($args, $argIndex, ParsedSwitches $ParsedSwit
}
// var_dump("Adding switch " . $switch->name);
- $ParsedSwitches->addSwitch($expectedOptions, $switch->name, $arg);
+ $parsedSwitches->addSwitch($expectedOptions, $switch->name, $arg);
}
// increment our counter through the args
@@ -174,8 +255,49 @@ protected function parseShortSwitch($args, $argIndex, ParsedSwitches $ParsedSwit
return $argIndex;
}
- protected function parseLongSwitch($args, $argIndex, ParsedSwitches $ParsedSwitches, DefinedSwitches $expectedOptions)
+ /**
+ * Take a long switch, and see if it is one that we are expecting
+ *
+ * This parser supports the following long switch formats:
+ *
+ * * --switch
+ * * --switch=<argument>
+ * * --switch <argument>
+ *
+ * These are all of the common long switch formats traditionally
+ * supported on UNIX-like systems
+ *
+ * All successfully parsed long switches are added to the
+ * $parsedSwitches object
+ *
+ * Any unexpected long switches, or any long switches that are
+ * missing their argument, will trigger an exception
+ *
+ * @param array $args
+ * The array of command-line arguments (normally $argv)
+ * @param int $argIndex
+ * The current index inside $args to search from
+ * @param ParsedSwitches $parsedSwitches
+ * The list of switches that we have already parsed
+ * @param DefinedSwitches $expectedOptions
+ * The list of command-line switches that we support
+ * @return int
+ * The new value of $argIndex
+ */
+ protected function parseLongSwitch($args, $argIndex, ParsedSwitches $parsedSwitches, DefinedSwitches $expectedOptions)
{
+ // catch programming errors
+ Contract::Preconditions(function() use ($args, $argIndex, $expectedOptions)
+ {
+ Contract::RequiresValue($args, is_array($args), '$args must be array');
+ Contract::RequiresValue($args, count($args) > 0, '$args cannot be an empty array');
+
+ Contract::RequiresValue($argIndex, is_integer($argIndex), '$argIndex must be an integer');
+ Contract::RequiresValue($argIndex, count($args) > $argIndex, '$argIndex cannot be beyond the end of $args');
+
+ Contract::RequiresValue($expectedOptions, count($expectedOptions->getSwitches()) > 0, '$expectedOptions must have some switches defined');
+ });
+
// $args[i] contains a long switch, and might contain
// a parameter too
$equalsPos = strpos($args[$argIndex], '=');
@@ -217,14 +339,54 @@ protected function parseLongSwitch($args, $argIndex, ParsedSwitches $ParsedSwitc
// increment to the next item in the list
$argIndex++;
- $ParsedSwitches->addSwitch($expectedOptions, $switch->name, $arg);
+ $parsedSwitches->addSwitch($expectedOptions, $switch->name, $arg);
// all done
return $argIndex;
}
+ /**
+ * Examine the command line for a (possibly optional) argument
+ * for a switch that we have just found on that command line
+ *
+ * @param array $args
+ * The array of command-line arguments (normally $argv)
+ * @param int $argIndex
+ * The current index inside $args to search from.
+ * This may be just beyond the end of the command-line args
+ * if the last command-line argument is a switch.
+ * @param int $startFrom
+ * The offset inside $args[$argIndex] where the argument string
+ * starts
+ * @param DefinedSwitch $switch
+ * The command-line switch that may need an argument
+ * @param string $switchSeen
+ * The actual switch we found on the command-line
+ * @return array(string, int)
+ * The argument we have parsed, plus the new value of $argIndex
+ */
protected function parseArgument($args, $argIndex, $startFrom, DefinedSwitch $switch, $switchSeen)
{
+ // catch programming errors
+ Contract::Preconditions(function() use ($args, $argIndex, $startFrom, $switchSeen)
+ {
+ Contract::RequiresValue($args, is_array($args), '$args must be array');
+ Contract::RequiresValue($args, count($args) > 0, '$args cannot be an empty array');
+
+ Contract::RequiresValue($argIndex, is_integer($argIndex), '$argIndex must be an integer');
+ Contract::RequiresValue($argIndex, count($args) >= $argIndex, '$argIndex cannot be more than +1 beyond the end of $args');
+
+ // this is a conditional test because it is legal
+ // for $args[$argindex] to be unset()
+ if (isset($args[$argIndex]))
+ {
+ Contract::RequiresValue($startFrom, $startFrom <= strlen($args[$argIndex]), '$startFrom cannot be more than +1 beyond the end of $args[$argIndex]');
+ }
+
+ Contract::RequiresValue($switchSeen, is_string($switchSeen), '$switchSeen must be a string');
+ Contract::RequiresValue($switchSeen, strlen($switchSeen) > 0, '$switchSeen cannot be an empty string');
+ });
+
// initialise the return value
$arg = null;
47 src/php/Phix_Project/CommandLineLib/DefinedArg.php
View
@@ -46,15 +46,49 @@
namespace Phix_Project\CommandLineLib;
+use Phix_Project\ContractLib\Contract;
use Phix_Project\ValidationLib\Validator;
+/**
+ * Represents the definition of a single argument for a single switch
+ */
class DefinedArg
{
+ /**
+ * The argument's name
+ *
+ * @var string
+ */
public $name;
+
+ /**
+ * The argument's description
+ *
+ * @var string
+ */
public $desc;
+
+ /**
+ * The default value of this argument, used if this argument isn't
+ * found when the command-line is parsed
+ *
+ * @var string
+ */
public $defaultValue = null;
+
+ /**
+ * Is this argument mandatory?
+ *
+ * @var boolean
+ */
public $isRequired = false;
+ /**
+ * How do we validate this argument before the calling app is
+ * allowed to see it?
+ *
+ * @var array(Validator)
+ */
protected $validators = array();
/**
@@ -99,7 +133,7 @@ public function setValidator(Validator $validator)
/**
* Is this argument optional?
- *
+ *
* @return boolean
*/
public function testIsOptional()
@@ -123,12 +157,19 @@ public function testIsRequired()
/**
* Does this arg have a specific validator defined?
- *
+ *
* @param string $validatorName
* @return boolean
*/
public function testMustValidateWith($validatorName)
{
+ // catch programming errors
+ Contract::Preconditions(function() use($validatorName)
+ {
+ Contract::RequiresValue($validatorName, is_string($validatorName), '$validatorName must be a string');
+ Contract::RequiresValue($validatorName, strlen($validatorName) > 0, '$validatorName cannot be an empty string');
+ });
+
foreach ($this->validators as $validator)
{
if (get_class($validator) == $validatorName)
@@ -167,7 +208,7 @@ public function testIsValid($value)
/**
* Remember the default value for this arg
- *
+ *
* @param mixed $value
* @return DefinedArg $this
*/
159 src/php/Phix_Project/CommandLineLib/DefinedSwitch.php
View
@@ -47,24 +47,103 @@
namespace Phix_Project\CommandLineLib;
use Phix_Project\ValidationLib\Validator;
+use Phix_Project\ContractLib\Contract;
+/**
+ * Represents a single definition of a single command-line switch
+ *
+ * We only need one DefinedSwitch to represent all of the valid forms of
+ * a single switch
+ */
class DefinedSwitch
{
+ /**
+ * The name of the switch
+ *
+ * @var string
+ */
public $name;
+
+ /**
+ * The switch's short description
+ *
+ * This can be used by the calling app, when outputing help to
+ * the user
+ *
+ * @var string
+ */
public $desc;
+
+ /**
+ * The switch's long description
+ *
+ * This can be used by the calling app, when outputing help to
+ * the user
+ *
+ * @var string
+ */
public $longdesc;
+
+ /**
+ * A list of the different short switches
+ *
+ * Any one switch can have multiple short switches. The classic
+ * example is '-?' and '-h', both of which do the same thing in
+ * well-behaved command-line applications
+ *
+ * @var array(string)
+ */
public $shortSwitches = array();
+
+ /**
+ * A list of the different long switches
+ *
+ * Any one switch can have multiple long switches. The classic
+ * example is '--?' and '--help', both of which do the same thing
+ * in well-behaved command-line applications
+ *
+ * @var array(string)
+ */
public $longSwitches = array();
/**
+ * The argument (if any) that this switch expects
+ *
* @var DefinedArg
*/
public $arg = null;
+
+ /**
+ * A bitset of flags that affect how this switch is parsed by
+ * the command-line parser
+ *
+ * @var int
+ */
public $flags = null;
+ /**
+ * The default behaviour flag
+ */
const FLAG_NONE = 0;
+
+ /**
+ * The behaviour flag for switches that can be repeated on the
+ * command-line
+ *
+ * The classic example of such a switch is '-vvv', where additional
+ * repeats make the app more and more verbose
+ */
const FLAG_REPEATABLE = 1;
+ /**
+ * Constructor
+ *
+ * @param string $name
+ * The switch's name, used as it's ID everywhere in the
+ * command-line parser
+ * @param string $desc
+ * The switch's short description
+ */
public function __construct($name, $desc)
{
$this->name = $name;
@@ -74,7 +153,7 @@ public function __construct($name, $desc)
/**
* Set it so that this switch is allowed to be repeated on
* the command-line by the user
- *
+ *
* @return DefinedSwitch
*/
public function setSwitchIsRepeatable()
@@ -92,6 +171,13 @@ public function setSwitchIsRepeatable()
*/
public function setWithShortSwitch($switch)
{
+ // catch programming errors
+ Contract::PreConditions(function() use ($switch)
+ {
+ Contract::RequiresValue($switch, is_string($switch), '$switch must be a string');
+ Contract::RequiresValue($switch, strlen($switch) > 0, '$switch cannot be an empty string');
+ });
+
// make sure the switch does not start with a '-'!!
if ($switch{0} == '-')
{
@@ -112,6 +198,13 @@ public function setWithShortSwitch($switch)
*/
public function setWithLongSwitch($switch)
{
+ // catch programming errors
+ Contract::PreConditions(function() use ($switch)
+ {
+ Contract::RequiresValue($switch, is_string($switch), '$switch must be a string');
+ Contract::RequiresValue($switch, strlen($switch) > 0, '$switch cannot be an empty string');
+ });
+
// make sure the switch does not start with a '-'!!
if ($switch{0} == '-')
{
@@ -132,6 +225,16 @@ public function setWithLongSwitch($switch)
*/
public function setWithOptionalArg($argName, $argDesc)
{
+ // catch programming errors
+ Contract::PreConditions(function() use ($argName, $argDesc)
+ {
+ Contract::RequiresValue($argName, is_string($argName), '$argName must be a string');
+ Contract::RequiresValue($argName, strlen($argName) > 0, '$argName cannot be an empty string');
+
+ Contract::RequiresValue($argDesc, is_string($argDesc), '$argDesc must be a string');
+ Contract::RequiresValue($argDesc, strlen($argDesc) > 0, '$argDesc cannot be an empty string');
+ });
+
$this->arg = new DefinedArg($argName, $argDesc);
$this->arg->setIsOptional();
return $this;
@@ -139,25 +242,62 @@ public function setWithOptionalArg($argName, $argDesc)
/**
* Add an argument that this switch requires
- *
+ *
* @param string $argName the name of the argument
* @param string $argDesc the argument's description
* @return DefinedSwitch
*/
public function setWithRequiredArg($argName, $argDesc)
{
+ // catch programming errors
+ Contract::PreConditions(function() use ($argName, $argDesc)
+ {
+ Contract::RequiresValue($argName, is_string($argName), '$argName must be a string');
+ Contract::RequiresValue($argName, strlen($argName) > 0, '$argName cannot be an empty string');
+
+ Contract::RequiresValue($argDesc, is_string($argDesc), '$argDesc must be a string');
+ Contract::RequiresValue($argDesc, strlen($argDesc) > 0, '$argDesc cannot be an empty string');
+ });
+
$this->arg = new DefinedArg($argName, $argDesc);
$this->arg->setIsRequired();
return $this;
}
+ /**
+ * Set the default value for the argument that this switch
+ * expects
+ *
+ * This only makes sense if the argument is optional
+ *
+ * @param string $value the default value for the argument
+ * @return DefinedSwitch
+ */
public function setArgHasDefaultValueOf($value)
{
+ // catch programming errors
+ Contract::PreConditions(function() use ($value)
+ {
+ Contract::RequiresValue($value, is_string($value), '$value must be a string');
+ Contract::RequiresValue($value, strlen($value) > 0, '$value cannot be an empty string');
+ });
+
$this->requireValidArg();
$this->arg->setDefaultValue($value);
return $this;
}
+ /**
+ * Add Validator object to the switch's argument
+ *
+ * The Validators are run, in the order that they've been added,
+ * when the command-line parser finds the argument for this switch.
+ * They are used to cover the very basics, but sophisticated
+ * Validators could be created and added too.
+ *
+ * @param Validator $validator
+ * @return DefinedSwitch
+ */
public function setArgValidator(Validator $validator)
{
$this->requireValidArg();
@@ -168,12 +308,19 @@ public function setArgValidator(Validator $validator)
/**
* Provide a longer description of this switch, to be shown during
* the output of extended help information
- *
+ *
* @param string $desc
* @return DefinedSwitch $this
*/
public function setLongDesc($desc)
{
+ // catch programming errors
+ Contract::PreConditions(function() use ($desc)
+ {
+ Contract::RequiresValue($desc, is_string($desc), '$desc must be a string');
+ Contract::RequiresValue($desc, strlen($desc) > 0, '$desc cannot be an empty string');
+ });
+
$this->longdesc = $desc;
return $this;
}
@@ -286,6 +433,12 @@ public function testIsRepeatable()
return false;
}
+ /**
+ * Return a list of the different forms of this switch, in a form
+ * that is suitable for building up human-readable help messages
+ *
+ * @return array
+ */
public function getHumanReadableSwitchList()
{
$return = array();
100 src/php/Phix_Project/CommandLineLib/DefinedSwitches.php
View
@@ -46,14 +46,35 @@
namespace Phix_Project\CommandLineLib;
+use Phix_Project\ContractLib\Contract;
+
+/**
+ * Container for all of the DefinedSwitches that we expect to find in the
+ * same place when parsing a command-line
+ */
class DefinedSwitches
{
- const FLAG_NONE = 0;
- const FLAG_HASPARAM = 1;
- const FLAG_CANBEDUPLICATED = 2;
-
+ /**
+ * A dynamic cache of all of the short switches that have been
+ * defined
+ *
+ * @var array(DefinedSwitch)
+ */
public $shortSwitches = array();
+
+ /**
+ * A dynamic cache of all of the long switches that have been
+ * defined
+ *
+ * @var array(DefinedSwitch)
+ */
public $longSwitches = array();
+
+ /**
+ * A list of all of the switches that have been defined
+ *
+ * @var array(DefinedSwitch)
+ */
public $switches = array();
/**
@@ -64,13 +85,22 @@ class DefinedSwitches
/**
* Add a switch to the list of allowed switches
- *
+ *
* @param string $name
* @param string $desc
- * @return DefinedSwitch
+ * @return DefinedSwitch
*/
public function addSwitch($name, $desc)
{
+ Contract::PreConditions(function() use ($name, $desc)
+ {
+ Contract::RequiresValue($name, is_string($name), '$name must be a string');
+ Contract::RequiresValue($name, strlen($name) > 0, '$name cannot be an empty string');
+
+ Contract::RequiresValue($desc, is_string($desc), '$desc must be a string');
+ Contract::RequiresValue($desc, strlen($desc) > 0, '$desc cannot be an empty string');
+ });
+
// note that our cache of introspected switches
// is now invalid
$this->allSwitchesLoaded = false;
@@ -83,6 +113,12 @@ public function addSwitch($name, $desc)
return $this->switches[$name];
}
+ /**
+ * Do we have the definition of a specific switch?
+ *
+ * @param string $switchName
+ * @return boolean
+ */
public function testHasSwitchByName($switchName)
{
if (isset($this->switches[$switchName]))
@@ -92,7 +128,13 @@ public function testHasSwitchByName($switchName)
return false;
}
-
+
+ /**
+ * Do we have a given short switch?
+ *
+ * @param string $switch
+ * @return boolean
+ */
public function testHasShortSwitch($switch)
{
// make sure the cache is complete
@@ -106,6 +148,12 @@ public function testHasShortSwitch($switch)
return false;
}
+ /**
+ * Get the definition of a given short switch
+ *
+ * @param string $switchName
+ * @return DefinedSwitch
+ */
public function getShortSwitch($switchName)
{
// make sure the cache is complete
@@ -119,6 +167,12 @@ public function getShortSwitch($switchName)
throw new \Exception("unknown switch $switch");
}
+ /**
+ * Do we have a definition for a given long switch?
+ *
+ * @param string $switch
+ * @return boolean
+ */
public function testHasLongSwitch($switch)
{
// make sure the cache is complete
@@ -132,6 +186,12 @@ public function testHasLongSwitch($switch)
return false;
}
+ /**
+ * Get the definition for a long switch
+ *
+ * @param string $switch
+ * @return DefinedSwitch
+ */
public function getLongSwitch($switch)
{
// make sure the cache is complete
@@ -145,6 +205,12 @@ public function getLongSwitch($switch)
throw new \Exception("unknown switch $switch");
}
+ /**
+ * Get the definition of a switch, by its name
+ *
+ * @param string $name
+ * @return DefinedSwitch
+ */
public function getSwitchByName($name)
{
if (isset($this->switches[$name]))
@@ -155,11 +221,22 @@ public function getSwitchByName($name)
throw new \Exception("unknown switch $switch");
}
+ /**
+ * Get the complete list of defined switches
+ *
+ * @return array(DefinedSwitch)
+ */
public function getSwitches()
{
return $this->switches;
}
+ /**
+ * Get a complete list of default values for all switches that
+ * take arguments
+ *
+ * @return array(string)
+ */
public function getDefaultValues()
{
$return = array();
@@ -179,6 +256,15 @@ public function getDefaultValues()
return $return;
}
+ /**
+ * Get an array of all of the switches in this definition, sorted
+ * into the right order to display in a help message
+ *
+ * This is for apps like phix, which display a help message that
+ * is designed to look like a UNIX manual page
+ *
+ * @return array(DefinedSwitch)
+ */
public function getSwitchesInDisplayOrder()
{
// turn the list into something that's suitably sorted
66 src/php/Phix_Project/CommandLineLib/ParsedSwitch.php
View
@@ -46,58 +46,97 @@
namespace Phix_Project\CommandLineLib;
+/**
+ * Represents a switch that has been parsed from the command-line
+ */
class ParsedSwitch
{
/**
- *
+ * The full definition for this switch
+ *
* @var DefinedSwitch
*/
public $definition = null;
/**
- *
+ * The name of this switch
+ *
* @var string
*/
public $name = null;
+
/**
- *
+ * Any arguments that have been passed to this switch
+ *
* @var array
*/
public $values = array();
/**
- *
+ * How many times this switch has been seen on the command-line
+ *
* @var int
*/
public $invokes = 0;
/**
- *
+ * For switches with optional arguments, are we using the argument's
+ * default value (because the argument wasn't supplied on the
+ * command line)?
+ *
* @var boolean
*/
public $isUsingDefaultValue = false;
+ /**
+ * Constructor
+ *
+ * @param DefinedSwitch $switch
+ */
public function __construct(DefinedSwitch $switch)
{
$this->definition = $switch;
$this->name = $switch->name;
}
+ /**
+ * Increment the counter of how many times the parser has seen
+ * this switch on the command-line
+ */
public function addToInvokeCount()
{
$this->invokes++;
}
+ /**
+ * Add an argument's value to the list that the parser has seen
+ * for this switch on the command-line
+ *
+ * @param string $value
+ */
public function addValue($value)
{
$this->values[] = $value;
}
+ /**
+ * Remember that the parser has not seen this switch's argument,
+ * and so we are having to use the argument's defined default
+ * value
+ */
public function setIsUsingDefaultValue()
{
$this->isUsingDefaultValue = true;
}
+ /**
+ * Run the switch's argument's validators against the list of
+ * values that the command-line parser found earlier
+ *
+ * @return array
+ * a list of error messages
+ * this list is empty if the validators all pass
+ */
public function validateValues()
{
$return = array();
@@ -123,6 +162,15 @@ public function validateValues()
return $return;
}
+ /**
+ * Get the first value seen for this switch
+ *
+ * This is a helper method for those switches that cannot be
+ * repeated on the command-line (and therefore, cannot have more
+ * than one parsed value)
+ *
+ * @return string
+ */
public function getFirstValue()
{
if (count($this->values) > 0)
@@ -132,7 +180,13 @@ public function getFirstValue()
return null;
}
-
+
+ /**
+ * Is this switch's argument using the default value, from the
+ * switch's argument's definition?
+ *
+ * @return boolean
+ */
public function testIsDefaultValue()
{
return $this->isUsingDefaultValue;
182 src/php/Phix_Project/CommandLineLib/ParsedSwitches.php
View
@@ -46,29 +46,76 @@
namespace Phix_Project\CommandLineLib;
+use Phix_Project\ContractLib\Contract;
+
+/**
+ * Container for all of the switches parsed by the command-line parser
+ */
class ParsedSwitches
{
/**
+ * A list of the switches that have been parsed, indexed by the
+ * name of the switch's DefinedSwitch
*
- * @var array
+ * @var array(ParsedSwitch)
*/
protected $switchesByName = array();
/**
+ * A list of the switches that have been parsed, in the order
+ * that the parser found them on the command line
*
* @var array
*/
protected $switchesByOrder = array();
+ /**
+ * Add a switch to this collection
+ *
+ * @param DefinedSwitches $expectedOptions
+ * A list of the switches that are allowed
+ * @param string $name
+ * The name of the switch to add to this collection
+ * @param $arg
+ * The value of any argument found by the parser
+ */
public function addSwitch(DefinedSwitches $expectedOptions, $name, $arg = true)
{
+ // catch programming errors
+ Contract::PreConditions(function() use ($name)
+ {
+ Contract::RequiresValue($name, is_string($name), '$name must be a string');
+ Contract::RequiresValue($name, strlen($name) > 0, '$name cannot be an empty string');
+ });
+
$this->requireValidExpectedSwitchName($expectedOptions, $name);
$this->addSwitchByName($expectedOptions, $name, $arg);
$this->addSwitchByOrder($expectedOptions, $name, $arg);
}
+ /**
+ * Add a switch to our 'by-name' indexed list
+ *
+ * @param DefinedSwitches $expectedOptions
+ * A list of the switches that are allowed
+ * @param string $name
+ * The name of the switch to add to this collection
+ * @param string $arg
+ * The value of any argument found by the parser
+ * @param boolean $isDefaultValue
+ * True if $arg is the default value for this switch
+ */
protected function addSwitchByName(DefinedSwitches $expectedOptions, $name, $arg, $isDefaultValue = false)
{
+ // catch programming errors
+ Contract::PreConditions(function() use ($name, $isDefaultValue)
+ {
+ Contract::RequiresValue($name, is_string($name), '$name must be a string');
+ Contract::RequiresValue($name, strlen($name) > 0, '$name cannot be an empty string');
+
+ Contract::RequiresValue($isDefaultValue, is_bool($isDefaultValue), '$isDefaultValue must be a boolean value');
+ });
+
if (!isset($this->switchesByName[$name]))
{
$this->switchesByName[$name] = new ParsedSwitch($expectedOptions->getSwitchByName($name));
@@ -82,8 +129,29 @@ protected function addSwitchByName(DefinedSwitches $expectedOptions, $name, $arg
}
}
+ /**
+ * Add a switch to our 'by-order' indexed list
+ *
+ * @param DefinedSwitches $expectedOptions
+ * A list of the switches that are allowed
+ * @param string $name
+ * The name of the switch to add to this collection
+ * @param type $arg
+ * The value of any argument found by the parser
+ * @param boolean $isDefaultValue
+ * True if $arg is the default value for this switch
+ */
protected function addSwitchByOrder(DefinedSwitches $expectedOptions, $name, $arg, $isDefaultValue = false)
{
+ // catch programming errors
+ Contract::PreConditions(function() use ($name, $isDefaultValue)
+ {
+ Contract::RequiresValue($name, is_string($name), '$name must be a string');
+ Contract::RequiresValue($name, strlen($name) > 0, '$name cannot be an empty string');
+
+ Contract::RequiresValue($isDefaultValue, is_bool($isDefaultValue), '$isDefaultValue must be a boolean value');
+ });
+
$parsedOption = new ParsedSwitch($expectedOptions->getSwitchByName($name));
$parsedOption->addToInvokeCount();
$parsedOption->addValue($arg);
@@ -95,19 +163,48 @@ protected function addSwitchByOrder(DefinedSwitches $expectedOptions, $name, $ar
$this->switchesByOrder[] = $parsedOption;
}
- public function addDefaultValue(DefinedSwitches $expectedOptions, $switchName, $value)
+ /**
+ * Add a switch that the user never supplied on the command line,
+ * but which does have a default value.
+ *
+ * This is used by the CommandLineParser to merge in all switches
+ * that have a default value, leaving it to us to make sure that
+ * we do not overwrite any switches that the CommandLineParser
+ * previously found
+ *
+ * @param DefinedSwitches $expectedOptions
+ * @param type $switchName
+ * @param type $value
+ * @return type
+ */
+
+ public function addSwitchWithDefaultValueIfUnseen(DefinedSwitches $expectedOptions, $switchName, $value)
{
$this->requireValidExpectedSwitchName($expectedOptions, $switchName);
+
// has this switch already been invoked by the user?
if (isset($this->switchesByName[$switchName]))
{
// yes
+ //
+ // we are only allowed to add switches that we
+ // have not seen before!
+ //
+ // NOTE: this is not a programming error when this
+ // occurs
return;
}
+
$this->addSwitchByName($expectedOptions, $switchName, $value, true);
$this->addSwitchByOrder($expectedOptions, $switchName, $value, true);
}
+ /**
+ * Have we already seen this switch?
+ *
+ * @param string $switchName
+ * @return boolean
+ */
public function testHasSwitch($switchName)
{
if (isset($this->switchesByName[$switchName]))
@@ -118,40 +215,99 @@ public function testHasSwitch($switchName)
return false;
}
+ /**
+ * Get a full list of the switches that we have
+ *
+ * The returned list is indexed by name, not by the order that
+ * we have seen the switches
+ *
+ * @return boolean
+ */
public function getSwitches()
{
return $this->switchesByName;
}
+ /**
+ * Return a switch
+ *
+ * Throws an Exception if the switch is one we have not seen yet
+ *
+ * @param string $switchName
+ * @return DefinedSwitch
+ */
public function getSwitch($switchName)
{
$this->requireValidSwitchName($switchName);
return $this->switchesByName[$switchName];
}
+ /**
+ * Get a list of the switches that we have seen
+ *
+ * The returned list is sorted in the order that we saw the switches
+ *
+ * @return array
+ */
public function getSwitchesByOrder()
{
return $this->switchesByOrder;
}
+ /**
+ * Get all of the values we've seen for a given switch's argument
+ *
+ * Multiple values will exist only for switches that are allowed
+ * to repeat on the command-line
+ *
+ * @param string $switchName
+ * @return array
+ */
public function getArgsForSwitch($switchName)
{
$this->requireValidSwitchName($switchName);
return $this->switchesByName[$switchName]->values;
}
+ /**
+ * Get the value we've seen for a given switch's argument
+ *
+ * This is the very useful method to call when you know that the
+ * switch you're interested in is never ever allowed to repeat
+ * on the command-line
+ *
+ * @param string $switchName
+ * @return string
+ */
public function getFirstArgForSwitch($switchName)
{
$this->requireValidSwitchName($switchName);
return $this->switchesByName[$switchName]->values[0];
}
+ /**
+ * How many times has a switch been seen on the command-line?
+ *
+ * @param string $switchName
+ * @return int
+ */
public function getInvokeCountForSwitch($switchName)
{
- $this->requireValidSwitchName($switchName);
+ if (!isset($this->switchesByName[$switchName]))
+ {
+ return 0;
+ }
+
return $this->switchesByName[$switchName]->invokes;
}
+ /**
+ * For each switch that we have seen, run any validators that have
+ * been defined for that switch.
+ *
+ * @return array
+ * The error messages returned by the validators
+ */
public function validateSwitchValues()
{
$return = array();
@@ -166,6 +322,15 @@ public function validateSwitchValues()
return $return;
}
+ /**
+ * Make sure that we have seen a named switch before
+ *
+ * If we have not seen the switch, throw an Exception. This is
+ * a helper method to make sure that we fail fast, fail hard if
+ * a programmatic error has occurred at runtime
+ *
+ * @param string $switchName
+ */
protected function requireValidSwitchName($switchName)
{
if (!$this->testHasSwitch($switchName))
@@ -174,6 +339,17 @@ protected function requireValidSwitchName($switchName)
}
}
+ /**
+ * Make sure that a named switch is in the list of defined switches
+ * that we are allowed to accept
+ *
+ * If the switch isn't in the list of defined switches, throw an
+ * Exception. This is a helper method to make sure that we fail
+ * fast, fail hard if a programmatic error has occurred at runtime.
+ *
+ * @param DefinedSwitches $expectedOptions
+ * @param string $switchName
+ */
protected function requireValidExpectedSwitchName(DefinedSwitches $expectedOptions, $switchName)
{
if (!$expectedOptions->testHasSwitchByName($switchName))
6 src/tests/unit-tests/bootstrap.php
View
@@ -25,3 +25,9 @@
psr0_autoloader_searchFirst(APP_LIBDIR);
psr0_autoloader_searchFirst(APP_TESTDIR);
psr0_autoloader_searchFirst(APP_TOPDIR);
+
+// step 4: enable ContractLib if it is available
+if (class_exists('Phix_Project\ContractLib\Contract'))
+{
+ \Phix_Project\ContractLib\Contract::EnforceWrappedContracts();
+}
87 src/tests/unit-tests/php/Phix_Project/CommandLineLib/ParsedSwitchesTest.php
View
@@ -62,18 +62,18 @@ public function testCanAddSwitch()
$expectedOptions->addSwitch($switchName, $switchDesc);
// create the ParsedSwitches object
- $ParsedSwitches = new ParsedSwitches();
- $ParsedSwitches->addSwitch($expectedOptions, $switchName);
+ $parsedSwitches = new ParsedSwitches();
+ $parsedSwitches->addSwitch($expectedOptions, $switchName);
// did it work?
- $this->assertTrue($ParsedSwitches->testHasSwitch($switchName));
+ $this->assertTrue($parsedSwitches->testHasSwitch($switchName));
// and what happens if we try to add a switch
// that has not been defined?
$caughtException = false;
try
{
- $ParsedSwitches->addSwitch($expectedOptions, 'harry');
+ $parsedSwitches->addSwitch($expectedOptions, 'harry');
}
catch (\Exception $e)
{
@@ -93,14 +93,14 @@ public function testCanCheckForSwitch()
$expectedOptions->addSwitch($switchName, $switchDesc);
// create the ParsedSwitches object
- $ParsedSwitches = new ParsedSwitches($expectedOptions);
- $ParsedSwitches->addSwitch($expectedOptions, $switchName);
+ $parsedSwitches = new ParsedSwitches($expectedOptions);
+ $parsedSwitches->addSwitch($expectedOptions, $switchName);
// did it work?
- $this->assertTrue($ParsedSwitches->testHasSwitch($switchName));
+ $this->assertTrue($parsedSwitches->testHasSwitch($switchName));
// if the switch is not there?
- $this->assertFalse($ParsedSwitches->testHasSwitch('harry'));
+ $this->assertFalse($parsedSwitches->testHasSwitch('harry'));
}
public function testCanGetSwitchByName()
@@ -120,13 +120,13 @@ public function testCanGetSwitchByName()
$switch2 = $expectedOptions->getSwitchByName($switchName2);
// create the ParsedSwitches object
- $ParsedSwitches = new ParsedSwitches($expectedOptions);
- $ParsedSwitches->addSwitch($expectedOptions, $switchName1);
- $ParsedSwitches->addSwitch($expectedOptions, $switchName2);
+ $parsedSwitches = new ParsedSwitches($expectedOptions);
+ $parsedSwitches->addSwitch($expectedOptions, $switchName1);
+ $parsedSwitches->addSwitch($expectedOptions, $switchName2);
// did it work?
- $retrievedSwitch1 = $ParsedSwitches->getSwitch($switchName1);
- $retrievedSwitch2 = $ParsedSwitches->getSwitch($switchName2);
+ $retrievedSwitch1 = $parsedSwitches->getSwitch($switchName1);
+ $retrievedSwitch2 = $parsedSwitches->getSwitch($switchName2);
$this->assertTrue($retrievedSwitch1 instanceof ParsedSwitch);
$this->assertEquals($switch1->name, $retrievedSwitch1->name);
@@ -140,7 +140,7 @@ public function testCanGetSwitchByName()
$caughtException = false;
try
{
- $ParsedSwitches->getSwitch('notdefined');
+ $parsedSwitches->getSwitch('notdefined');
}
catch (\Exception $e)
{
@@ -157,11 +157,11 @@ public function testCanGetSwitchArgValues()
$expectedOptions->addSwitch('fred', 'trout')
->setWithOptionalArg('<fish>', 'which kind of fish you like');
- $ParsedSwitches = new ParsedSwitches();
- $ParsedSwitches->addSwitch($expectedOptions, 'fred', 'salmon');
+ $parsedSwitches = new ParsedSwitches();
+ $parsedSwitches->addSwitch($expectedOptions, 'fred', 'salmon');
// can we do this?
- $retrievedArgs = $ParsedSwitches->getArgsForSwitch('fred');
+ $retrievedArgs = $parsedSwitches->getArgsForSwitch('fred');
// did it work?
$this->assertTrue(is_array($retrievedArgs));
@@ -177,11 +177,11 @@ public function testCanGetSwitchArgFirstValue()
$expectedOptions->addSwitch('fred', 'trout')
->setWithOptionalArg('<fish>', 'which kind of fish you like');
- $ParsedSwitches = new ParsedSwitches();
- $ParsedSwitches->addSwitch($expectedOptions, 'fred', 'salmon');
+ $parsedSwitches = new ParsedSwitches();
+ $parsedSwitches->addSwitch($expectedOptions, 'fred', 'salmon');
// can we do this?
- $retrievedArg = $ParsedSwitches->getFirstArgForSwitch('fred');
+ $retrievedArg = $parsedSwitches->getFirstArgForSwitch('fred');
// did it work?
$this->assertEquals('salmon', $retrievedArg);
@@ -195,11 +195,11 @@ public function testReturnsTrueIfSwitchArgFirstValueMissing()
$expectedOptions->addSwitch('fred', 'trout')
->setWithOptionalArg('<fish>', 'which kind of fish you like');
- $ParsedSwitches = new ParsedSwitches();
- $ParsedSwitches->addSwitch($expectedOptions, 'fred');
+ $parsedSwitches = new ParsedSwitches();
+ $parsedSwitches->addSwitch($expectedOptions, 'fred');
// can we do this?
- $retrievedArg = $ParsedSwitches->getFirstArgForSwitch('fred');
+ $retrievedArg = $parsedSwitches->getFirstArgForSwitch('fred');
// did it work?
$this->assertTrue($retrievedArg);
@@ -222,12 +222,12 @@ public function testCanGetAllSwitches()
$switch2 = $expectedOptions->getSwitchByName($switchName2);
// create the ParsedSwitches object
- $ParsedSwitches = new ParsedSwitches($expectedOptions);
- $ParsedSwitches->addSwitch($expectedOptions, $switchName1);
- $ParsedSwitches->addSwitch($expectedOptions, $switchName2);
+ $parsedSwitches = new ParsedSwitches($expectedOptions);
+ $parsedSwitches->addSwitch($expectedOptions, $switchName1);
+ $parsedSwitches->addSwitch($expectedOptions, $switchName2);
// did it work?
- $switches = $ParsedSwitches->getSwitches();
+ $switches = $parsedSwitches->getSwitches();
$this->assertEquals(2, count($switches));
$this->assertSame($switch1, $switches[$switchName1]->definition);
$this->assertSame($switch2, $switches[$switchName2]->definition);
@@ -244,18 +244,19 @@ public function testCanAddRepeatedSwitches()
$expectedOptions->addSwitch($switchName, $switchDesc);
// create the ParsedSwitches object
- $ParsedSwitches = new ParsedSwitches($expectedOptions);
+ $parsedSwitches = new ParsedSwitches($expectedOptions);
+ $this->assertEquals(0, $parsedSwitches->getInvokeCountForSwitch($switchName));
// repeat the switch
- $ParsedSwitches->addSwitch($expectedOptions, $switchName);
- $ParsedSwitches->addSwitch($expectedOptions, $switchName);
+ $parsedSwitches->addSwitch($expectedOptions, $switchName);
+ $parsedSwitches->addSwitch($expectedOptions, $switchName);
// did it work?
- $switches = $ParsedSwitches->getSwitches();
- $retrievedArgs = $ParsedSwitches->getArgsForSwitch($switchName);
+ $switches = $parsedSwitches->getSwitches();
+ $retrievedArgs = $parsedSwitches->getArgsForSwitch($switchName);
$this->assertEquals(1, count($switches));
$this->assertEquals(2, count($retrievedArgs));
- $this->assertEquals(2, $ParsedSwitches->getInvokeCountForSwitch($switchName));
+ $this->assertEquals(2, $parsedSwitches->getInvokeCountForSwitch($switchName));
}
public function testCanAddRepeatedSwitchesWithArguments()
@@ -282,19 +283,19 @@ public function testCanAddRepeatedSwitchesWithArguments()
);
// add the switch
- $ParsedSwitches = new ParsedSwitches();
+ $parsedSwitches = new ParsedSwitches();
foreach ($args as $arg)
{
- $ParsedSwitches->addSwitch($expectedOptions, $switchName, $arg);
+ $parsedSwitches->addSwitch($expectedOptions, $switchName, $arg);
}
// did it work?
- $switches = $ParsedSwitches->getSwitches();
+ $switches = $parsedSwitches->getSwitches();
$this->assertEquals(1, count($switches));
$this->assertEquals($switchName, $switches[$switchName]->name);
- $this->assertEquals(count($args), count($ParsedSwitches->getArgsForSwitch($switchName)));
- $this->assertEquals(count($args), $ParsedSwitches->getInvokeCountForSwitch($switchName));
- $this->assertEquals($args, $ParsedSwitches->getArgsForSwitch($switchName));
+ $this->assertEquals(count($args), count($parsedSwitches->getArgsForSwitch($switchName)));
+ $this->assertEquals(count($args), $parsedSwitches->getInvokeCountForSwitch($switchName));
+ $this->assertEquals($args, $parsedSwitches->getArgsForSwitch($switchName));
}
public function testCanValidateAllSwitchValuesInOneGo()
@@ -310,12 +311,12 @@ public function testCanValidateAllSwitchValuesInOneGo()
->setArgValidator(new MustBeInteger());
// add the parsed results
- $ParsedSwitches = new ParsedSwitches();
- $ParsedSwitches->addSwitch($expectedOptions, 'fred', 'trout');
- $ParsedSwitches->addSwitch($expectedOptions, 'harry', 'salmon');
+ $parsedSwitches = new ParsedSwitches();
+ $parsedSwitches->addSwitch($expectedOptions, 'fred', 'trout');
+ $parsedSwitches->addSwitch($expectedOptions, 'harry', 'salmon');
// now, can we validate or not?
- $results = $ParsedSwitches->validateSwitchValues();
+ $results = $parsedSwitches->validateSwitchValues();
// what happened?
$this->assertTrue(is_array($results));
Please sign in to comment.
Something went wrong with that request. Please try again.