Skip to content
This repository was archived by the owner on Jan 10, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
zsh_history
.idea
vendor
composer.lock
65 changes: 65 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
FROM ubuntu:18.04
ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && apt-get install -y software-properties-common locales

RUN locale-gen en_US.UTF-8
ENV LANG=en_US.UTF-8

RUN apt-add-repository ppa:git-core/ppa -y && \
apt-get update && \
apt-get install -y --force-yes \
nano \
git \
curl \
vim \
wget \
zsh \
iputils-ping \
gnupg-agent \
rsync

RUN useradd -ms /bin/zsh gowork

WORKDIR /home/gowork

RUN apt-add-repository -y ppa:ondrej/php && apt-get update && apt-get install -y --force-yes \
php7.3-curl \
php7.3-gd \
php7.3-intl \
php7.3-mysql \
php7.3-xml \
php7.3-mbstring \
php7.3-bcmath \
php7.3-mongo \
php7.3-zip \
php7.3-opcache \
php7.3-bz2 \
php7.3-gmp \
php7.3-redis \
php7.3-cli

RUN curl -sS https://getcomposer.org/installer | php && \
mv composer.phar /usr/local/bin/composer

USER gowork

RUN git clone git://github.com/robbyrussell/oh-my-zsh.git /home/gowork/.oh-my-zsh \
&& cp /home/gowork/.oh-my-zsh/templates/zshrc.zsh-template /home/gowork/.zshrc \
&& sed -i.bak 's/robbyrussell/nebirhos/' /home/gowork/.zshrc

RUN echo "plugins=(git symfony symfony2 composer yarn)" >> /home/gowork/.zshrc

# from https://hub.docker.com/r/themattrix/develop/~/dockerfile/
RUN git clone https://github.com/junegunn/fzf.git /home/gowork/.fzf \
&& (cd /home/gowork/.fzf) \
&& (yes | /home/gowork/.fzf/install)

RUN mkdir /home/gowork/.ssh && \
echo "StrictHostKeyChecking no\n" >> /home/gowork/.ssh/config

RUN echo "export FZF_DEFAULT_OPTS='--no-height --no-reverse'" >> /home/gowork/.zshrc

RUN mkdir /home/gowork/current
RUN touch /home/gowork/.zsh_history
WORKDIR /home/gowork/current
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "gowork/phpstan-gw-values",
"description": "GW Values extensions for PHPStan",
"license": ["MIT"],
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "~7.1",
"phpstan/phpstan": "^0.10",
"nikic/php-parser": "^4.0"
},
"require-dev": {
"gowork/values": "^0.2",
"phpunit/phpunit": "^7.0"
},
"autoload": {
"psr-4": {
"GW\\PHPStan\\GwValues\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"tests\\GW\\PHPStan\\GwValues\\": "tests/"
}
}
}
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "2"

services:
develop:
build: .
image: values_console
volumes:
- ./:/home/gowork/current
- ~/.ssh/id_rsa:/home/gowork/.ssh/id_rsa
- ~/.ssh/id_rsa.pub:/home/gowork/.ssh/id_rsa.pub
- ./zsh_history:/home/gowork/.zsh_history
- ~/.gitconfig:/home/gowork/.gitconfig
- ~/.ssh/known_hosts:/home/gowork/.ssh/known_hosts
57 changes: 57 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
services:
-
class: GW\PHPStan\GwValues\ArrayValueDynamicReturn
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: GW\PHPStan\GwValues\ArrayValueMapDynamicReturn
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: GW\PHPStan\GwValues\ArrayValueToArrayDynamicReturn
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: GW\PHPStan\GwValues\ArrayValuePopDynamicReturn
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: GW\PHPStan\GwValues\ArrayValueJoinDynamicReturn
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: GW\PHPStan\GwValues\ArrayValueReduceDynamicReturn
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: GW\PHPStan\GwValues\ArrayValueToAssocValueDynamicReturn
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: GW\PHPStan\GwValues\ArrayValueToStringValueDynamicReturn
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: GW\PHPStan\GwValues\ArrayValueChunkDynamicReturn
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: GW\PHPStan\GwValues\AssocValueToArrayValueDynamicReturn
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: GW\PHPStan\GwValues\WrapDynamicReturn
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
-
class: GW\PHPStan\GwValues\ArrayValueCallbackRule
tags:
- phpstan.rules.rule
-
class: GW\PHPStan\GwValues\ArrayValuePushRule
tags:
- phpstan.rules.rule
-
class: GW\PHPStan\GwValues\ArrayReduceCallbackRule
tags:
- phpstan.rules.rule
88 changes: 88 additions & 0 deletions src/ArrayReduceCallbackRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php declare(strict_types=1);

namespace GW\PHPStan\GwValues;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\Native\NativeParameterReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ClosureType;
use PHPStan\Type\VerbosityLevel;

final class ArrayReduceCallbackRule implements Rule
{
/** @var RuleLevelHelper */
private $ruleLevelHelper;

public function __construct(RuleLevelHelper $ruleLevelHelper)
{
$this->ruleLevelHelper = $ruleLevelHelper;
}

public function getNodeType(): string
{
return Node\Expr\MethodCall::class;
}

/**
* @return string[] errors
*/
public function processNode(Node $methodCall, Scope $scope): array
{
if (!$methodCall instanceof Node\Expr\MethodCall) {
return [];
}

$identifier = $methodCall->name;

if (!$identifier instanceof Node\Identifier) {
return [];
}

if ($methodCall->name->name !== 'reduce') {
return [];
}

$valueType = TypeHelper::searchArrayValueType($scope->getType($methodCall->var));

if (!$valueType instanceof ArrayValueType) {
return [];
}

$errors = [];
$parameterIndex = 1;

$attribute = $methodCall->args[0]->value;
$callableType = $scope->getType($attribute);

if ($callableType instanceof ClosureType) {
/** @var NativeParameterReflection[] $parameters */
$parameters = $callableType->getParameters();
$parameter = $parameters[$parameterIndex];
$parameterType = $parameters[$parameterIndex]->getType();
$argumentValueType = $valueType->innerType();

$accepts = $this->ruleLevelHelper->accepts(
$parameterType,
$argumentValueType,
$scope->isDeclareStrictTypes()
);

if (!$accepts) {
$errors[] = \sprintf(
'Parameter #%d %s of method ' . $methodCall->name->name . ' callback expects %s, %s given.',
$parameterIndex + 1,
sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName()),
$parameterType->describe(VerbosityLevel::typeOnly()),
$argumentValueType->describe(
$parameterType->isCallable()->yes() ? VerbosityLevel::value()
: VerbosityLevel::typeOnly()
)
);
}
}

return $errors;
}
}
103 changes: 103 additions & 0 deletions src/ArrayValueCallbackRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php declare(strict_types=1);

namespace GW\PHPStan\GwValues;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\Native\NativeParameterReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ClosureType;
use PHPStan\Type\VerbosityLevel;

final class ArrayValueCallbackRule implements Rule
{
/** @var RuleLevelHelper */
private $ruleLevelHelper;

public function __construct(RuleLevelHelper $ruleLevelHelper)
{
$this->ruleLevelHelper = $ruleLevelHelper;
}

public function getNodeType(): string
{
return Node\Expr\MethodCall::class;
}

/**
* @return string[] errors
*/
public function processNode(Node $methodCall, Scope $scope): array
{
if (!$methodCall instanceof Node\Expr\MethodCall) {
return [];
}

$identifier = $methodCall->name;

if (!$identifier instanceof Node\Identifier) {
return [];
}

if (!\in_array(
$identifier->name,
['map', 'filter', 'each', 'sort', 'unique', 'any', 'every'],
true
)) {
return [];
}

$numberOfParameters = 1;

if (\in_array($methodCall->name->name, ['unique', 'sort'], true)) {
$numberOfParameters = 2;
}

$valueType = TypeHelper::searchArrayValueType($scope->getType($methodCall->var));

if (!$valueType instanceof ArrayValueType) {
return [];
}

$errors = [];

for ($parameterIndex = 0; $parameterIndex < $numberOfParameters; $parameterIndex++) {
if (!isset($methodCall->args[$parameterIndex])) {
continue;
}

$attribute = $methodCall->args[$parameterIndex]->value;
$callableType = $scope->getType($attribute);

if ($callableType instanceof ClosureType) {
/** @var NativeParameterReflection[] $parameters */
$parameters = $callableType->getParameters();
$parameter = $parameters[$parameterIndex];
$parameterType = $parameters[$parameterIndex]->getType();
$argumentValueType = $valueType->innerType();

$accepts = $this->ruleLevelHelper->accepts(
$parameterType,
$argumentValueType,
$scope->isDeclareStrictTypes()
);

if (!$accepts) {
$errors[] = \sprintf(
'Parameter #%d %s of method ' . $methodCall->name->name . ' callback expects %s, %s given.',
$parameterIndex + 1,
sprintf('%s$%s', $parameter->isVariadic() ? '...' : '', $parameter->getName()),
$parameterType->describe(VerbosityLevel::typeOnly()),
$argumentValueType->describe(
$parameterType->isCallable()->yes() ? VerbosityLevel::value()
: VerbosityLevel::typeOnly()
)
);
}
}
}

return $errors;
}
}
Loading