Giter Site home page Giter Site logo

phpstan-beberlei-assert's Introduction

PHPStan - PHP Static Analysis Tool

PHPStan

Build Status Latest Stable Version Total Downloads License PHPStan Enabled


PHPStan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even before you write tests for the code. It moves PHP closer to compiled languages in the sense that the correctness of each line of the code can be checked before you run the actual line.

Read more about PHPStan »

Try out PHPStan on the on-line playground! »

Sponsors

TheCodingMachine     Private Packagist
CDN77     Blackfire.io
iO     Amezmo
ShipMonk     Togetter
RightCapital     ContentKing
ZOL     Psyonix
Shopware     Craft CMS
Worksome     campoint AG
Crisp.nl     Inviqa
GetResponse     EdgeNext
Fame Helsinki

You can now sponsor my open-source work on PHPStan through GitHub Sponsors.

Does GitHub already have your 💳? Do you use PHPStan to find 🐛 before they reach production? Send a couple of 💸 a month my way too. Thank you!

One-time donations through PayPal are also accepted. To request an invoice, contact me through e-mail.

Documentation

All the documentation lives on the phpstan.org website:

PHPStan Pro

PHPStan Pro is a paid add-on on top of open-source PHPStan Static Analysis Tool with these premium features:

  • Web UI for browsing found errors, you can click and open your editor of choice on the offending line.
  • Continuous analysis (watch mode): scans changed files in the background, refreshes the UI automatically.

Try it on PHPStan 0.12.45 or later by running it with the --pro option. You can create an account either by following the on-screen instructions, or by visiting account.phpstan.com.

After 30-day free trial period it costs 7 EUR for individuals monthly, 70 EUR for teams (up to 25 members). By paying for PHPStan Pro, you're supporting the development of open-source PHPStan.

You can read more about it on PHPStan's website.

Code of Conduct

This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.

Contributing

Any contributions are welcome. PHPStan's source code open to pull requests lives at phpstan/phpstan-src.

phpstan-beberlei-assert's People

Contributors

acasademont avatar dependabot[bot] avatar emilmassey avatar herndlm avatar kayneth avatar kocal avatar localheinz avatar lookyman avatar ondrejmirtes avatar renovate-bot avatar renovate[bot] avatar ruudk avatar simpod avatar staabm avatar tomasvotruba avatar villfa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

phpstan-beberlei-assert's Issues

Assert::that()->numeric() being marked as "always evaluate to true"

For the following code:

class Bar
{
    public function doBar(string $s)
    {
        Assert::that($s)->numeric();
    }
}

We're always getting the error:

# Call to method Assert\AssertionChain::numeric() will always evaluate to true.

I don't believe this is quite right, since we might have non-numeric strings, correct? Or are we doing something wrong?

Add support for integerish assertion

I'd be great if the integerish assertion would also work, as its value is guaranteed to be either a string|float|int (almost like the scalar assertion minus the boolean type). I can work on a PR if needed.

Assertion::isInstanceOf is not working as expected

Given controller:

# src/Controller/EventController.php
<?php
declare(strict_types=1);

namespace App\Controller;

use App\Entity\User;
use Assert\Assertion;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;

class EventController
{
    /**
     * @param UserInterface|User $user
     *
     * @return array
     * @Route("/events", name="event_list")
     * @Template(template="event/list.html.twig")
     *
     */
    public function __invoke(UserInterface $user): array
    {
        Assertion::isInstanceOf($user, User::class);
        $team = $user->getTeams();
        ...

Given User class:

<?php
declare(strict_types=1);

namespace App\Entity;

...
use FOS\UserBundle\Model\User as BaseUser; # this one implements \Symfony\Component\Security\Core\User\UserInterface
...

class User extends BaseUser
{
    /**
     * @return Collection|Team[]
     */
    public function getTeams()
    {
        ...
    }

Given phpstan.neon

parameters:
    level: 3
    paths:
        - %currentWorkingDirectory%/src
    includes:
        - vendor/phpstan/phpstan-beberlei-assert/extension.neon
    excludes_analyse:
        - %currentWorkingDirectory%/src/Migrations/Version*.php
    parameters:
        symfony:
            container_xml_path: %rootDir%/../../../var/cache/dev/srcDevDebugProjectContainer.xml

Given phpstan-output:

 ------ ----------------------------------------------------------------- 
  Line   Controller/EventController.php                                   
 ------ ----------------------------------------------------------------- 
  25     Call to an undefined method                                      
         Symfony\Component\Security\Core\User\UserInterface::getTeams().  
 ------ ----------------------------------------------------------------- 

Expected phpstan-output:

 [OK] No errors  

Am I missing something?

PS: I have the same issue with https://github.com/phpstan/phpstan-webmozart-assert. I will open an issue there if this one gets confirmed and if it's not a misconfiguration or misuse on my side.

Assert::that conditions are not working as expected

Given this case https://phpstan.org/r/ea5dfda5-33b6-49e7-8e5f-1db4214533e4 I'd expect that method withAssertwill work just like withIf. PhpStan will however throw following error

Offset 'host' does not exist on false|array('scheme' => string, ?'host' => string, ?'port' => int, ?'user' => string, ?'pass' => string, ?'query' => string, ?'fragment' => string). |

I know that phpstan/phpstan-beberlei-assert is not integrated in the web-tester, but the same will happen locally when it is.

Assertion::inArray w/ constant array support

Should specify type to a union of the array values.

$format = $_GET['foo'];

Assertion::inArray($format, [ // or Assertion::choice
    ExportFormat::FORMAT_CSV,
    ExportFormat::FORMAT_EXCEL,
]);

\PhpStan\dumpType($format); // ExportFormat::FORMAT_CSV|ExportFormat::FORMAT_EXCEL

Chainable types do not work

the following code:

<?php

declare(strict_types=1);

use Assert\Assert;

class Result
{
    private ?float $percent;

    private function __construct()
    {
    }

    /** @param array<string, mixed> $payload */
    public static function fromQueryResults(array $payload): self
    {
        Assert::that($payload['percent'])
            ->nullOr()
            ->string()
            ->numeric();

        $floatOrNull = static fn (?string $value): ?float => null === $value ? null : (float) $value;

        $instance = new self();
        $instance->percent = $floatOrNull($payload['percent']);

        return $instance;
    }
}

Produces the error:
Parameter #1 $value of closure expects string|null, float|int|string|null given.

The type of $payload['percent'] should be infered to string|null, not float|int|string|null

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

composer
composer.json
  • php ^7.2 || ^8.0
  • phpstan/phpstan ^1.10
  • beberlei/assert ^3.3.0
  • nikic/php-parser ^4.13.0
  • php-parallel-lint/php-parallel-lint ^1.2
  • phpstan/phpstan-phpunit ^1.0
  • phpstan/phpstan-strict-rules ^1.0
  • phpunit/phpunit ^9.5
github-actions
.github/workflows/build.yml
  • actions/checkout v4
  • shivammathur/setup-php v2
  • actions/checkout v4
  • actions/checkout v4
  • shivammathur/setup-php v2
  • actions/checkout v4
  • shivammathur/setup-php v2
  • actions/checkout v4
  • shivammathur/setup-php v2
.github/workflows/create-tag.yml
  • actions/checkout v4
  • WyriHaximus/github-action-get-previous-tag v1
  • WyriHaximus/github-action-next-semvers v1
  • rickstaa/action-create-tag v1
  • rickstaa/action-create-tag v1
.github/workflows/lock-closed-issues.yml
  • dessant/lock-threads v5
.github/workflows/release-toot.yml
  • cbrgm/mastodon-github-action v2
.github/workflows/release-tweet.yml
  • Eomm/why-don-t-you-tweet v1
.github/workflows/release.yml
  • actions/checkout v4
  • metcalfc/changelog-generator v4.3.1
  • actions/create-release v1

  • Check this box to trigger a request for Renovate to run again on this repository

Invalid Assertion::eq evaluation

    private function first()
    {
        $var = getenv('env');
        Assertion::eq($var, 'value');
        $this->second($var);
    }

    private function second(string $needsString)
    {

    }

Returns

Parameter #1 $needsString of method ::second() expects string, string|false given.

Even though we know the possible set of types for $var is string when passed into second(). Or am I wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.