Skip to content

Commit

Permalink
more codesniff
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Schwarz committed Mar 10, 2017
1 parent 283f0c8 commit 8508eca
Show file tree
Hide file tree
Showing 7 changed files with 620 additions and 300 deletions.
2 changes: 1 addition & 1 deletion src/LesserPhp/Color/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function toRGB(array $color)
* @param int $max
* @param int $min
*
* @return mixed
* @return int
*/
public function clamp($v, $max = 1, $min = 0)
{
Expand Down
4 changes: 2 additions & 2 deletions src/LesserPhp/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ protected function compileRoot($root)

/**
* @param $block
* @param $out
* @param \stdClass $out
*
* @throws \LesserPhp\Exception\GeneralException
*/
Expand Down Expand Up @@ -1674,7 +1674,7 @@ protected function makeOutputBlock($type, $selectors = null)
}

/**
* @param $parent
* @param \LesserPhp\NodeEnv $parent
* @param null $block
*
* @return \LesserPhp\NodeEnv
Expand Down
60 changes: 48 additions & 12 deletions src/LesserPhp/Library/Assertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,27 @@
*/
class Assertions
{

private $coerce;

/**
* Assertions constructor.
*
* @param \LesserPhp\Library\Coerce $coerce
*/
public function __construct(Coerce $coerce)
{
$this->coerce = $coerce;
}

public function assertColor($value, $error = "expected color value")
/**
* @param array $value
* @param string $error
*
* @return array
* @throws \LesserPhp\Exception\GeneralException
*/
public function assertColor(array $value, $error = 'expected color value')
{
$color = $this->coerce->coerceColor($value);
if ($color === null) {
Expand All @@ -34,27 +47,42 @@ public function assertColor($value, $error = "expected color value")
return $color;
}

public function assertNumber($value, $error = "expecting number")
/**
* @param array $value
* @param string $error
*
* @return mixed
* @throws \LesserPhp\Exception\GeneralException
*/
public function assertNumber(array $value, $error = 'expecting number')
{
if ($value[0] === "number") {
if ($value[0] === 'number') {
return $value[1];
}
throw new GeneralException($error);
}

public function assertArgs($value, $expectedArgs, $name = "")
/**
* @param array $value
* @param int $expectedArgs
* @param string $name
*
* @return array
* @throws \LesserPhp\Exception\GeneralException
*/
public function assertArgs(array $value, $expectedArgs, $name = '')
{
if ($expectedArgs == 1) {
if ($expectedArgs === 1) {
return $value;
} else {
if ($value[0] !== "list" || $value[1] !== ",") {
if ($value[0] !== 'list' || $value[1] !== ',') {
throw new GeneralException('expecting list');
}
$values = $value[2];
$numValues = count($values);
if ($expectedArgs != $numValues) {
if ($expectedArgs !== $numValues) {
if ($name) {
$name .= ": ";
$name .= ': ';
}
throw new GeneralException("${name}expecting $expectedArgs arguments, got $numValues");
}
Expand All @@ -63,16 +91,24 @@ public function assertArgs($value, $expectedArgs, $name = "")
}
}

public function assertMinArgs($value, $expectedMinArgs, $name = "")
/**
* @param array $value
* @param int $expectedMinArgs
* @param string $name
*
* @return array
* @throws \LesserPhp\Exception\GeneralException
*/
public function assertMinArgs(array $value, $expectedMinArgs, $name = '')
{
if ($value[0] !== "list" || $value[1] !== ",") {
throw new GeneralException("expecting list");
if ($value[0] !== 'list' || $value[1] !== ',') {
throw new GeneralException('expecting list');
}
$values = $value[2];
$numValues = count($values);
if ($expectedMinArgs > $numValues) {
if ($name) {
$name .= ": ";
$name .= ': ';
}

throw new GeneralException("${name}expecting at least $expectedMinArgs arguments, got $numValues");
Expand Down
32 changes: 22 additions & 10 deletions src/LesserPhp/Library/Coerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,23 @@ class Coerce
];


// coerce a value for use in color operation
public function coerceColor($value)
/**
* coerce a value for use in color operation
*
* @param array $value
*
* @return array|null
*/
public function coerceColor(array $value)
{
switch ($value[0]) {
case 'color':
return $value;
case 'raw_color':
$c = ["color", 0, 0, 0];
$colorStr = substr($value[1], 1);
$c = ['color', 0, 0, 0];
$colorStr = mb_substr($value[1], 1);
$num = hexdec($colorStr);
$width = strlen($colorStr) === 3 ? 16 : 256;
$width = mb_strlen($colorStr) === 3 ? 16 : 256;

for ($i = 3; $i > 0; $i--) { // 3 2 1
$t = $num % $width;
Expand All @@ -204,14 +210,20 @@ public function coerceColor($value)
return null;
}

// make something string like into a string
public function coerceString($value)
/**
* make something string like into a string
*
* @param array $value
*
* @return array|null
*/
public function coerceString(array $value)
{
switch ($value[0]) {
case "string":
case 'string':
return $value;
case "keyword":
return ["string", "", [$value[1]]];
case 'keyword':
return ['string', '', [$value[1]]];
}

return null;
Expand Down

0 comments on commit 8508eca

Please sign in to comment.