memio/linter
Memio Linter provides a set of Memio Validator constraints to lint Memio models for syntax and structural issues. Use it standalone or as part of the Memio code generator to validate arguments, methods, contracts, objects, files, and collections.
composer require memio/linter memio/validator memio/model
Build class):
use Memio\Build;
$models = Build::models()->fromDirectory(app_path('Models'));
$validator = Build::linter()->validate($models);
if ($validator->hasErrors()) {
foreach ($validator->errors() as $error) {
echo "Error: {$error->message()}\n";
}
exit(1);
}
php artisan memio:linter if a custom command exists).If not using Memio, extract constraints manually:
use Memio\Validator\Constraint\MethodCannotBeAbstractAndHaveBody;
use Memio\Validator\ModelValidator\MethodValidator;
// Example: Validate a Laravel model class
$reflectionClass = new ReflectionClass(App\Models\User::class);
$methodValidator = new MethodValidator(
new ArgumentValidator(),
new CollectionValidator()
);
$methodValidator->add(new MethodCannotBeAbstractAndHaveBody());
$errors = $methodValidator->validate($reflectionClass);
Constraint-Based Validation:
$validator = new Validator();
$validator->add(new ObjectValidator(/* ... */));
$validator->add(new ContractValidator(/* ... */));
trait MemioLinterTrait {
public function validateWithMemioLinter() {
$validator = new Validator();
// Add constraints...
return $validator->validate(new ReflectionClass($this));
}
}
CI/CD Integration:
- name: Memio Linter
run: |
php vendor/bin/memio linter --directory=app/Models --fail-on-error
phpstan for layered validation:
- name: PHPStan + Memio Linter
run: |
vendor/bin/phpstan analyse --level=max
vendor/bin/memio linter --fail-on-error
Custom Constraints:
fillable fields:
use Memio\Validator\Constraint\ConstraintInterface;
class FillableFieldsCannotBePrivate implements ConstraintInterface {
public function validate($value) {
// Logic to check fillable fields in Eloquent models
}
}
Build::linter() for zero-config validation of Memio-generated models.MethodCannotBeAbstractAndHaveBody) via reflection.make:model to validate new models:
// In a service provider
Model::creating(function ($model) {
$validator = new Validator();
$validator->add(new ObjectValidator(/* ... */));
if ($validator->validate(new ReflectionClass($model))->hasErrors()) {
throw new \RuntimeException("Model validation failed");
}
});
Memio Dependency Lock-In:
ReflectionClass).False Positives in Laravel:
CollectionCannotHaveNameDuplicates may flag Eloquent relationships incorrectly.$validator->ignore(new CollectionCannotHaveNameDuplicates());
Performance Overhead:
vendor/bin/memio linter --directory=app/Models --changed-only
CI/CD Flakiness:
- uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
phpspec to test constraints in isolation:
make phpspec arg='--format pretty --filter="MethodCannotBeAbstractAndHaveBody"'
$validator->setVerbose(true);
LaravelConstraint class to bridge Memio and Laravel:
class EloquentFillableConstraint implements ConstraintInterface {
public function validate($model) {
$reflection = new ReflectionClass($model);
// Custom logic for Laravel fillable fields
}
}
$constraints = config('memio.linter.constraints');
foreach ($constraints as $constraint) {
$validator->add(new $constraint());
}
ModelCreated events:
Model::created(function ($model) {
$validator = new Validator();
$validator->add(new ObjectValidator(/* ... */));
$validator->validate(new ReflectionClass($model));
});
Makefile). For Laravel, consider:
docker run --rm -v $(pwd):/app composer require memio/linter
php.ini settings (e.g., opcache.enable=0) match Memio’s requirements.vendor/bin/phpstan analyse --level=max --no-progress
vendor/bin/memio linter --fail-on-error
CONSTRAINTS.md file to explain why each constraint exists (e.g., "No abstract methods with bodies to prevent runtime errors").MethodCannotBeAbstractAndHaveBody) before enabling all.How can I help you explore Laravel packages today?