Skip to content

Commit

Permalink
Merge pull request #1 from answear/allow-php8-add-github-workflows
Browse files Browse the repository at this point in the history
Allow php8, add github workflows, phpstan, drop support for sf4, drop support for php < 7.4
  • Loading branch information
lukasz-falda authored Sep 22, 2022
2 parents a7e2e2e + 58755e5 commit ba8a0b6
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 28 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: "Continuous Integration"

on:
pull_request: null

jobs:
phpunit:
name: "PHPUnit"
runs-on: "ubuntu-20.04"

strategy:
matrix:
php-version:
- "7.4"
- "8.0"
- "8.1"
deps:
- "normal"
include:
- deps: "low"
php-version: "7.4"

steps:
- name: "Checkout"
uses: "actions/checkout@v2"
with:
fetch-depth: 2

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php-version }}"

- name: "Cache dependencies installed with composer"
uses: "actions/cache@v2"
with:
path: "~/.composer/cache"
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: "php-${{ matrix.php-version }}-composer-locked-"

- name: "Update dependencies with composer"
run: "composer update --no-interaction --no-progress --no-suggest"
if: "${{ matrix.deps == 'normal' }}"

- name: "Install lowest possible dependencies with composer"
run: "composer update --no-interaction --no-progress --no-suggest --prefer-dist --prefer-lowest"
if: "${{ matrix.deps == 'low' }}"

- name: "Run PHPUnit"
run: "vendor/bin/phpunit"
39 changes: 39 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: "Static Analysis"

on:
pull_request: null

jobs:
phpstan:
name: "PHPStan"
runs-on: "ubuntu-20.04"

strategy:
matrix:
php-version:
- "7.4"
- "8.0"
- "8.1"
steps:
- name: "Checkout"
uses: "actions/checkout@v2"
with:
fetch-depth: 2

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php-version }}"

- name: "Cache dependencies installed with composer"
uses: "actions/cache@v2"
with:
path: "~/.composer/cache"
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: "php-${{ matrix.php-version }}-composer-locked-"

- name: "Install dependencies with composer"
run: "composer install --no-interaction --no-progress"

- name: "Run a static analysis with phpstan/phpstan"
run: "vendor/bin/phpstan analyse --error-format=github"
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ cache:
- $HOME/.composer/cache/files

php:
- 7.2
- 7.3
- 7.4
- 8.0
- 8.1

before_install:
- composer self-update
Expand Down
21 changes: 14 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,30 @@
}
},
"require": {
"php": "^7.2.5",
"symfony/form": "^4.4|^5.0",
"symfony/translation": "^4.4|^5.0",
"symfony/serializer": "^4.4|^5.0",
"symfony/contracts": "^1.0|^2.1"
"php": "^7.4|^8.0",
"symfony/form": "^5.4",
"symfony/translation": "^5.4",
"symfony/serializer": "^5.4",
"symfony/contracts": "^v2.3.1",
"symfony/validator": "^5.4"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.6.2",
"dealerdirect/phpcodesniffer-composer-installer": "^v0.7.2",
"escapestudios/symfony2-coding-standard": "^3.0",
"phpunit/phpunit": "^8.0",
"phpcompatibility/php-compatibility": "^9.3"
"phpcompatibility/php-compatibility": "^9.3",
"phpstan/phpstan": "^1.8"
},
"replace": {
"limenius/liform": "*"
},
"scripts": {
"test": "phpunit",
"cs": "phpcs"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Method Limenius\\\\Liform\\\\Resolver\\:\\:resolve\\(\\) should return array but returns Limenius\\\\Liform\\\\Transformer\\\\TransformerInterface\\.$#"
count: 1
path: src/Limenius/Liform/Resolver.php
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- phpstan-baseline.neon

parameters:
level: 5
paths:
- %rootDir%/../../../src
2 changes: 1 addition & 1 deletion src/Limenius/Liform/FormUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static function isTypeInAncestry(FormInterface $form, $type)
*/
public static function type(FormInterface $form)
{
return $form->getConfig()->getType()->getName();
return $form->getConfig()->getType()->getBlockPrefix();
}

/**
Expand Down
11 changes: 8 additions & 3 deletions src/Limenius/Liform/Guesser/ValidatorGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Form\Guess\ValueGuess;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Constraints\Length;

/**
* @author Nacho Martín <[email protected]>
Expand All @@ -41,21 +44,23 @@ public function guessMinLength(string $class, string $property)
public function guessMinLengthForConstraint(Constraint $constraint)
{
switch (get_class($constraint)) {
case 'Symfony\Component\Validator\Constraints\Length':
case Length::class:
if (is_numeric($constraint->min)) {
return new ValueGuess($constraint->min, Guess::HIGH_CONFIDENCE);
}
break;
case 'Symfony\Component\Validator\Constraints\Type':
case Type::class:
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
}
break;
case 'Symfony\Component\Validator\Constraints\Range':
case Range::class:
if (is_numeric($constraint->min)) {
return new ValueGuess(strlen((string) $constraint->min), Guess::LOW_CONFIDENCE);
}
break;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ private function convertFormToArray(FormInterface $data)
private function getErrorMessage(FormError $error)
{
if (null !== $error->getMessagePluralization()) {
return $this->translator->transChoice($error->getMessageTemplate(), $error->getMessagePluralization(), $error->getMessageParameters(), 'validators');
return $this->translator->trans(
$error->getMessageTemplate(),
array_merge(
$error->getMessageParameters(),
['%count%' => $error->getMessagePluralization()]
),
'validators'
);
}

return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Limenius\Liform\Serializer\Normalizer;

use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Limenius\Liform\FormUtil;
Expand Down Expand Up @@ -43,22 +44,22 @@ public function supportsNormalization($data, $format = null)

/**
* Gets the values of the form
* @param Form $form
* @param FormView $formView
* @param Form|FormInterface $form
* @param FormView $formView
*
* @return mixed
*/
private function getValues(Form $form, FormView $formView)
private function getValues(FormInterface $form, FormView $formView)
{
if (!empty($formView->children)) {
if (in_array('choice', FormUtil::typeAncestry($form)) &&
$formView->vars['expanded']
) {
if ($formView->vars['multiple']) {
return $this->normalizeMultipleExpandedChoice($formView);
} else {
return $this->normalizeExpandedChoice($formView);
}

return $this->normalizeExpandedChoice($formView);
}
// Force serialization as {} instead of []
$data = (object) array();
Expand All @@ -71,15 +72,15 @@ private function getValues(Form $form, FormView $formView)
}

return (array) $data;
} else {
// handle separatedly the case with checkboxes, so the result is
// true/false instead of 1/0
if (isset($formView->vars['checked'])) {
return $formView->vars['checked'];
}
}

return $formView->vars['value'];
// handle separatedly the case with checkboxes, so the result is
// true/false instead of 1/0
if (isset($formView->vars['checked'])) {
return $formView->vars['checked'];
}

return $formView->vars['value'];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Limenius/Liform/Transformer/AbstractTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class AbstractTransformer implements TransformerInterface
* @param TranslatorInterface $translator
* @param FormTypeGuesserInterface|null $validatorGuesser
*/
public function __construct(TranslatorInterface $translator, FormTypeGuesserInterface $validatorGuesser = null)
public function __construct(TranslatorInterface $translator, ?FormTypeGuesserInterface $validatorGuesser = null)
{
$this->translator = $translator;
$this->validatorGuesser = $validatorGuesser;
Expand Down Expand Up @@ -75,7 +75,7 @@ protected function applyExtensions(array $extensions, FormInterface $form, array
*
* @return array
*/
protected function addCommonSpecs(FormInterface $form, array $schema, $extensions = [], $widget)
protected function addCommonSpecs(FormInterface $form, array $schema, array $extensions = [], $widget = null)
{
$schema = $this->addLabel($form, $schema);
$schema = $this->addAttr($form, $schema);
Expand Down
5 changes: 5 additions & 0 deletions src/Limenius/Liform/Transformer/StringTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Limenius\Liform\Transformer;

use Limenius\Liform\FormUtil;
use Limenius\Liform\Guesser\ValidatorGuesser;
use Symfony\Component\Form\FormInterface;

/**
Expand Down Expand Up @@ -75,6 +76,10 @@ protected function addMinLength(FormInterface $form, array $schema)
return $schema;
}

if (!$this->validatorGuesser instanceof ValidatorGuesser) {
throw new \RuntimeException('Invalid ValidatorGuesser class.');
}

$minLengthGuess = $this->validatorGuesser->guessMinLength($class, $form->getName());
$minLength = $minLengthGuess ? $minLengthGuess->getValue() : null;

Expand Down

0 comments on commit ba8a0b6

Please sign in to comment.