Skip to content

Commit

Permalink
Local build: rely only on docker (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk authored Jun 11, 2024
1 parent aff7d78 commit e485ec4
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 71 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.php-cs-fixer.php export-ignore
/Dockerfile export-ignore
/docker-compose.yml export-ignore
/Makefile export-ignore
/composer-require-checker.json export-ignore
/phpstan.neon export-ignore
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/.phpunit.cache/
/vendor/
/coverage/
/.env
/.php-cs-fixer.cache
/.phpunit.result.cache
/composer.lock
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM php:8.3

ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

RUN install-php-extensions @composer pcov

ARG USER_ID
ARG GROUP_ID

RUN groupadd --gid ${GROUP_ID} code \
&& useradd --create-home --shell /bin/bash --uid ${USER_ID} --gid code code

USER code
27 changes: 17 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
CSFIX_PHP_BIN=PHP_CS_FIXER_IGNORE_ENV=1 php8.2
PHP_BIN=php8.2 -d zend.assertions=1
COMPOSER_BIN=$(shell command -v composer)
DOCKER_PHP_EXEC := docker compose run php
PHP_BIN=php -d zend.assertions=1

all: csfix static-analysis test
@echo "Done."

vendor: composer.json
$(PHP_BIN) $(COMPOSER_BIN) update
$(PHP_BIN) $(COMPOSER_BIN) bump
touch vendor
.env: /etc/passwd /etc/group Makefile
printf "USER_ID=%s\nGROUP_ID=%s\n" `id --user "${USER}"` `id --group "${USER}"` > .env

vendor: .env docker-compose.yml Dockerfile composer.json
docker compose build --pull
$(DOCKER_PHP_EXEC) composer update
$(DOCKER_PHP_EXEC) composer bump
touch --no-create $@

.PHONY: csfix
csfix: vendor
$(CSFIX_PHP_BIN) vendor/bin/php-cs-fixer fix -v $(arg)
$(DOCKER_PHP_EXEC) vendor/bin/php-cs-fixer fix -v $(arg)

.PHONY: static-analysis
static-analysis: vendor
$(PHP_BIN) vendor/bin/phpstan analyse $(PHPSTAN_ARGS)
$(DOCKER_PHP_EXEC) $(PHP_BIN) vendor/bin/phpstan analyse --memory-limit=512M $(PHPSTAN_ARGS)

.PHONY: test
test: vendor
$(PHP_BIN) vendor/bin/phpunit $(PHPUNIT_ARGS)
$(DOCKER_PHP_EXEC) $(PHP_BIN) vendor/bin/phpunit $(PHPUNIT_ARGS)

.PHONY: clean
clean:
git clean -dfX
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
],
"require": {
"php": "~8.2.0 || ~8.3.0",
"nikic/php-parser": "^4.18.0 || ^5.0.0",
"phpstan/phpstan": "^1.10.59"
"nikic/php-parser": "^4.19.1 || ^5.0.2",
"phpstan/phpstan": "^1.11.4"
},
"require-dev": {
"nette/di": "^3.2.1",
"nette/di": "^3.2.2",
"nette/neon": "^3.4.1",
"phpstan/phpstan-phpunit": "^1.3.16",
"phpunit/phpunit": "^11.0.4",
"phpstan/phpstan-phpunit": "^1.4.0",
"phpunit/phpunit": "^11.2.1",
"slam/php-cs-fixer-extensions": "^3.10.0"
},
"autoload": {
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
php:
build:
context: .
args:
USER_ID: ${USER_ID}
GROUP_ID: ${GROUP_ID}
volumes:
- .:${PWD}
working_dir: ${PWD}
8 changes: 5 additions & 3 deletions lib/AccessGlobalVariableWithinContextRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<Variable>
Expand Down Expand Up @@ -42,7 +44,7 @@ public function getNodeType(): string
return Variable::class;
}

/** @return string[] */
/** @return list<RuleError> */
public function processNode(Node $node, Scope $scope): array
{
if (! \is_string($node->name)) {
Expand All @@ -64,12 +66,12 @@ public function processNode(Node $node, Scope $scope): array

$modelBaseClassOrInterface = $this->broker->getClass($this->contextBaseClassOrInterface);

return [\sprintf(
return [RuleErrorBuilder::message(\sprintf(
'Class %s %s %s and uses $%s: accessing globals in this context is considered an anti-pattern',
$classReflection->getDisplayName(),
$modelBaseClassOrInterface->isInterface() ? 'implements' : 'extends',
$this->contextBaseClassOrInterface,
$node->name
)];
))->identifier('globalAccess.outOfContext')->build()];
}
}
8 changes: 5 additions & 3 deletions lib/AccessStaticPropertyWithinModelContextRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<StaticPropertyFetch>
Expand All @@ -31,7 +33,7 @@ public function getNodeType(): string
return StaticPropertyFetch::class;
}

/** @return string[] */
/** @return list<RuleError> */
public function processNode(Node $node, Scope $scope): array
{
if (! $node->class instanceof Node\Name || ! $node->name instanceof Node\VarLikeIdentifier) {
Expand Down Expand Up @@ -59,13 +61,13 @@ public function processNode(Node $node, Scope $scope): array

$modelBaseClassOrInterface = $this->broker->getClass($this->modelBaseClassOrInterface);

return [\sprintf(
return [RuleErrorBuilder::message(\sprintf(
'Class %s %s %s and uses %s::$%s: accessing a singleton in this context is considered an anti-pattern',
$classReflection->getDisplayName(),
$modelBaseClassOrInterface->isInterface() ? 'implements' : 'extends',
$this->modelBaseClassOrInterface,
$this->singletonAccessor,
(string) $node->name
)];
))->identifier('singletonAccess.outOfContext')->build()];
}
}
18 changes: 11 additions & 7 deletions lib/ClassNotationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<ClassLike>
Expand All @@ -30,23 +32,23 @@ public function getNodeType(): string
return ClassLike::class;
}

/** @return string[] */
/** @return list<RuleError> */
public function processNode(Node $node, Scope $scope): array
{
$messages = [];
$nodeIdentifier = $node->name;
if (null === $nodeIdentifier) {
return $messages;
return [];
}
$name = $nodeIdentifier->name;
if (\str_starts_with($name, 'AnonymousClass')) {
return $messages;
return [];
}
if (null === $node->namespacedName) {
return $messages;
return [];
}

$fqcn = $node->namespacedName->toString();
$messages = [];
$fqcn = $node->namespacedName->toString();
if ($node instanceof Interface_) {
if (! \preg_match('/Interface$/', $name)) {
$messages[] = \sprintf('Interface %s should end with "Interface" suffix.', $fqcn);
Expand Down Expand Up @@ -75,6 +77,8 @@ public function processNode(Node $node, Scope $scope): array
}
}

return $messages;
return \array_map(static function (string $message): RuleError {
return RuleErrorBuilder::message($message)->identifier('classNotation')->build();
}, $messages);
}
}
6 changes: 4 additions & 2 deletions lib/GotoRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PhpParser\Node\Stmt\Goto_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<Goto_>
Expand All @@ -19,9 +21,9 @@ public function getNodeType(): string
return Goto_::class;
}

/** @return string[] */
/** @return list<RuleError> */
public function processNode(Node $node, Scope $scope): array
{
return ['No goto, cmon!'];
return [RuleErrorBuilder::message('No goto, cmon!')->identifier('goto.forbidden')->build()];
}
}
10 changes: 8 additions & 2 deletions lib/MissingClosureParameterTypehintRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PhpParser\Node\Expr\Closure;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<Closure>
Expand All @@ -19,7 +21,7 @@ public function getNodeType(): string
return Closure::class;
}

/** @return string[] */
/** @return list<RuleError> */
public function processNode(Node $node, Scope $scope): array
{
$messages = [];
Expand All @@ -29,7 +31,11 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$messages[] = \sprintf('Parameter #%d $%s of anonymous function has no typehint.', 1 + $index, $param->var->name);
$messages[] = RuleErrorBuilder::message(\sprintf(
'Parameter #%d $%s of anonymous function has no typehint.',
1 + $index,
$param->var->name,
))->identifier('typehintMissing.anonymousFunction')->build();
}

return $messages;
Expand Down
8 changes: 6 additions & 2 deletions lib/NotNow/NoDateWithoutSecondArgumentRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<FuncCall>
Expand All @@ -27,7 +29,7 @@ public function getNodeType(): string
return FuncCall::class;
}

/** @return string[] */
/** @return list<RuleError> */
public function processNode(Node $node, Scope $scope): array
{
if (! $node->name instanceof Node\Name) {
Expand All @@ -42,6 +44,8 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return ['Calling date() without the second parameter is forbidden, rely on a clock abstraction like lcobucci/clock'];
return [RuleErrorBuilder::message(
'Calling date() without the second parameter is forbidden, rely on a clock abstraction like lcobucci/clock',
)->identifier('datecall.implicitTime.forbidden')->build()];
}
}
26 changes: 12 additions & 14 deletions lib/NotNow/NoRelativeDateTimeInterfaceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PhpParser\Node\Expr\New_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\TypeWithClassName;

Expand All @@ -22,7 +24,7 @@ public function getNodeType(): string
return New_::class;
}

/** @return string[] */
/** @return list<RuleError> */
public function processNode(Node $node, Scope $scope): array
{
$type = $scope->getType($node);
Expand All @@ -37,12 +39,10 @@ public function processNode(Node $node, Scope $scope): array
$args = $node->getArgs();

if (0 === \count($args)) {
return [
\sprintf(
'Instantiating %s without the first argument is forbidden, rely on a clock abstraction like lcobucci/clock',
DateTimeInterface::class
),
];
return [RuleErrorBuilder::message(\sprintf(
'Instantiating %s without the first argument is forbidden, rely on a clock abstraction like lcobucci/clock',
DateTimeInterface::class,
))->identifier('newdate.implicitTime.forbidden')->build()];
}

$argType = $scope->getType($args[0]->value);
Expand All @@ -55,12 +55,10 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return [
\sprintf(
'Instantiating %s with relative datetime "%s" is forbidden, rely on a clock abstraction like lcobucci/clock',
DateTimeInterface::class,
$value
),
];
return [RuleErrorBuilder::message(\sprintf(
'Instantiating %s with relative datetime "%s" is forbidden, rely on a clock abstraction like lcobucci/clock',
DateTimeInterface::class,
$value,
))->identifier('newdate.relativeTime.forbidden')->build()];
}
}
14 changes: 7 additions & 7 deletions lib/NotNow/NoRelativeStrtotimeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ConstantScalarType;

/**
Expand All @@ -28,7 +30,7 @@ public function getNodeType(): string
return FuncCall::class;
}

/** @return string[] */
/** @return list<RuleError> */
public function processNode(Node $node, Scope $scope): array
{
if (! $node->name instanceof Node\Name) {
Expand All @@ -55,11 +57,9 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return [
\sprintf(
'Calling strtotime() with relative datetime "%s" without the second argument is forbidden, rely on a clock abstraction like lcobucci/clock',
$value
),
];
return [RuleErrorBuilder::message(\sprintf(
'Calling strtotime() with relative datetime "%s" without the second argument is forbidden, rely on a clock abstraction like lcobucci/clock',
$value,
))->identifier('strtotimecall.implicitTime.forbidden')->build()];
}
}
Loading

0 comments on commit e485ec4

Please sign in to comment.