Skip to content

Commit

Permalink
Update function names and type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlmoon committed Jan 17, 2020
1 parent 3d30ebe commit bd52bce
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
26 changes: 13 additions & 13 deletions src/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,27 @@ public function __construct(array $extra_options = []) {
public function check($value, array $constraint) {

if ($this->debug) {
$this->validate_constraint($constraint);
$this->validateConstraint($constraint);
}

$value = $this->check_default($value, $constraint);
$value = $this->checkDefault($value, $constraint);

if ($this->is_null($value, $constraint)) {
if ($this->isNull($value, $constraint)) {
return null;
}


if (in_array($constraint["type"], $this->primitive_types)) {

$new_value = $this->filter_primitive($value, $constraint["type"], $constraint);
$new_value = $this->filterPrimitive($value, $constraint["type"], $constraint);

} elseif (isset($this->abstract_types[$constraint["type"]])) {

$new_value = $this->filter_abstract($value, $this->abstract_types[$constraint["type"]], $constraint);
$new_value = $this->filterAbstract($value, $this->abstract_types[$constraint["type"]], $constraint);

} elseif (is_object($value) || class_exists($constraint["type"])) {

$new_value = $this->filter_class($value, $constraint["type"]);
$new_value = $this->filterClass($value, $constraint["type"]);

} else {

Expand Down Expand Up @@ -156,7 +156,7 @@ public function check($value, array $constraint) {
* @param array $constraint A constraint array
* @return mixed
*/
public function filter_primitive($value, $type, $constraint) {
public function filterPrimitive($value, string $type, array $constraint) {

$expectation = "valid $type";

Expand Down Expand Up @@ -250,9 +250,9 @@ public function filter_primitive($value, $type, $constraint) {
* @param array $constraint A constraint array
* @return mixed
*/
public function filter_abstract($value, $class, $constraint) {
public function filterAbstract($value, string $class, array $constraint) {
$new_value = $class::filter(
$this->filter_primitive($value, $class::PRIMITIVE, $constraint),
$this->filterPrimitive($value, $class::PRIMITIVE, $constraint),
$constraint,
$this
);
Expand All @@ -275,7 +275,7 @@ public function filter_abstract($value, $class, $constraint) {
* @param string $class Class name
* @return mixed
*/
public function filter_class($value, $class) {
public function filterClass($value, string $class) {
if (!($value instanceof $class)) {
if (is_array($value) && is_a($class, "\ArrayObject", true)) {
$obj = new $class();
Expand All @@ -300,7 +300,7 @@ public function filter_class($value, $class) {
* @param array $constraint A constraint array
* @return mixed
*/
protected function check_default($value, array $constraint) {
protected function checkDefault($value, array $constraint) {
/**
* If we got a null value but the column has a default, change it
* and throw a warning on dev as that is bad form.
Expand All @@ -321,7 +321,7 @@ protected function check_default($value, array $constraint) {
* @param array $constraint A constraint array
* @return boolean [description]
*/
protected function is_null($value, array $constraint) {
protected function isNull($value, array $constraint): bool {
$is_null = false;
/**
* If we are still null after the default check, we need to just skip
Expand Down Expand Up @@ -351,7 +351,7 @@ protected function is_null($value, array $constraint) {
* @return bool
* @throws \LogicException
*/
public function validate_constraint(array $constraint) {
public function validateConstraint(array $constraint): bool {
foreach ($constraint as $option => $value) {
if (!array_key_exists($option, $this->valid_constraint)) {
throw new \LogicException("Invalid constraint option $option", 1);
Expand Down
6 changes: 3 additions & 3 deletions src/ConstraintException.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ConstraintException extends \UnexpectedValueException {
* @param integer $code A unique code for this thrown exception
* @param \Throwable|null $previous A previously thrown exception which was caught
*/
public function __construct ($value, $expected, $example, $code = 0, \Throwable $previous = null) {
public function __construct ($value, string $expected, string $example, int $code = 0, \Throwable $previous = null) {
if (!empty($expected)) {
$this->expected = $expected;
}
Expand Down Expand Up @@ -66,7 +66,7 @@ public function __construct ($value, $expected, $example, $code = 0, \Throwable
*
* @return string
*/
public function getExpected () {
public function getExpected (): string {
return $this->expected;
}

Expand All @@ -75,7 +75,7 @@ public function getExpected () {
*
* @return string
*/
public function getExample () {
public function getExample (): string {
return $this->example;
}
}
10 changes: 5 additions & 5 deletions tests/ConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testConstraintValidation() {
];
$dc = new Constraint();
$this->assertTrue(
$dc->validate_constraint($valid_constraint)
$dc->validateConstraint($valid_constraint)
);
}

Expand All @@ -86,7 +86,7 @@ public function testConstraintValidation() {
public function testConstraintValidationExceptionTypes() {
$dc = new Constraint();
$this->assertTrue(
$dc->validate_constraint(["type" => 1])
$dc->validateConstraint(["type" => 1])
);
}

Expand All @@ -97,14 +97,14 @@ public function testConstraintValidationExceptionTypes() {
public function testConstraintValidationExceptionKeys() {
$dc = new Constraint();
$this->assertTrue(
$dc->validate_constraint(["foo" => 1])
$dc->validateConstraint(["foo" => 1])
);
}

public function testConstraintValidationExtraValues() {
$dc = new Constraint(["foo" => 0]);
$this->assertTrue(
$dc->validate_constraint(["foo" => 1])
$dc->validateConstraint(["foo" => 1])
);
}

Expand All @@ -115,7 +115,7 @@ public function testConstraintValidationExtraValues() {
public function testConstraintValidationExceptionExtraKeys() {
$dc = new Constraint(["foo" => 0]);
$this->assertTrue(
$dc->validate_constraint(["foo" => ""])
$dc->validateConstraint(["foo" => ""])
);
}

Expand Down

0 comments on commit bd52bce

Please sign in to comment.