Skip to content

Commit 3b7f589

Browse files
committed
feat: PHP Code Analysis Tool Beta
1 parent ed1a11f commit 3b7f589

File tree

15 files changed

+2998
-0
lines changed

15 files changed

+2998
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
.DS_Store
3+
/docker-compose.yml
4+
/vendor

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!--- BEGIN HEADER -->
2+
# Changelog
3+
4+
All notable changes to this project will be documented in this file.
5+
6+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
7+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
8+
9+
---
10+
<!--- END HEADER -->
11+
12+
## [0.0.2](https://github.com/justcoded/dockerize-php-code-analysis/compare/ed1a11f0ca55f603eb7df6312a31d4e08031d9e8...v0.0.2) (2023-09-20)
13+
14+
---
15+

Dockerfile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
FROM php:8.2-cli
2+
3+
# |--------------------------------------------------------------------------
4+
# | Basic tools setup
5+
# |--------------------------------------------------------------------------
6+
# |
7+
RUN apt update && apt install -y git unzip make
8+
RUN apt autoremove
9+
10+
ENV COMPOSER_ALLOW_SUPERUSER=1
11+
ENV COMPOSER_MEMORY_LIMIT=-1
12+
13+
# |--------------------------------------------------------------------------
14+
# | Composer
15+
# |--------------------------------------------------------------------------
16+
# |
17+
RUN cd ~ \
18+
&& curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php \
19+
&& HASH=`curl -sS https://composer.github.io/installer.sig` \
20+
&& echo $HASH \
21+
&& php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
22+
&& php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer \
23+
&& composer
24+
25+
RUN composer global config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
26+
RUN composer global require symplify/coding-standard symplify/easy-coding-standard friendsofphp/php-cs-fixer kubawerlos/php-cs-fixer-custom-fixers squizlabs/php_codesniffer slevomat/coding-standard
27+
28+
ENV PATH="~/.composer/vendor/bin:${PATH}"
29+
30+
RUN mkdir -p /app
31+
RUN mkdir -p /codebase
32+
COPY ecs.php /app
33+
34+
WORKDIR /codebase
35+
36+
CMD ["/root/.composer/vendor/bin/ecs", "--config", "/app/ecs.php"]

Makefile

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
.PHONY: default
2+
3+
SHELL=/bin/bash
4+
include bin/colors-xterm.mk
5+
6+
PROJECT_NAME := Dockerized PHP Code Analysis Tool
7+
SQL_DB_MAX_WAIT := 120
8+
9+
default:
10+
@echo '${PROJECT_NAME}'
11+
@echo 'Run ${FG_YELLOW}make help${FG_RESET} to find out available commands'
12+
13+
include bin/vars.mk
14+
include bin/utils.mk
15+
include bin/changelog.mk
16+
include bin/code.mk
17+
18+
##
19+
# @command code.test.check Check code in test directory.
20+
##
21+
code.test.check:
22+
${MAKE} build
23+
${ECS_DR} \
24+
-c "ecs check --config /app/ecs.php test"
25+
26+
##
27+
# @command code.test.fix Fix code in test directory.
28+
##
29+
code.test.fix:
30+
${MAKE} build
31+
${ECS_DR} \
32+
-c "ecs check --config /app/ecs.php --fix test"
33+
34+
##
35+
# @command build Build docker image
36+
##
37+
build:
38+
docker-compose build
39+

bin/changelog.mk

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.PHONY: chl chl-first chl-amend
2+
3+
CONV_CHL_IMAGE := justcoded/php-conventional-changelog:latest
4+
CONV_CHL_DR := docker run -it --rm --volume "$$PWD":/codebase ${CONV_CHL_IMAGE} bash
5+
CONV_CHL_CMD := conventional-changelog --config bin/changelog.php
6+
7+
##
8+
# @command chl Generate changelog based on conventional commits
9+
##
10+
chl:
11+
${CONV_CHL_DR} \
12+
-c "${CONV_CHL_CMD}"
13+
14+
##
15+
# @command chl-first Generate changelog based on conventional commits, first version
16+
##
17+
chl-first:
18+
${CONV_CHL_DR} \
19+
-c "${CONV_CHL_CMD} --first-release"

bin/changelog.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* @see: https://github.com/marcocesarato/php-conventional-changelog/blob/main/docs/config.md
4+
*/
5+
6+
return [
7+
'path' => 'CHANGELOG.md',
8+
'headerTitle' => 'Changelog',
9+
'headerDescription' => 'All notable changes to this project will be documented in this file.
10+
11+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
12+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
13+
14+
---',
15+
'ignoreTypes' => ['build', 'chore', 'ci', 'docs', 'perf', 'refactor', 'revert', 'style', 'test', 'bug'],
16+
17+
'hiddenHash' => true,
18+
];

bin/code.mk

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
.PHONY: code.check code.check.dirty code.fix code.fix.dirty code.check.diff code.fix.diff
2+
3+
version ?= 0.2.0
4+
branch ?= develop
5+
6+
#ECS_IMAGE := hub.jcdev.net:24000/php-code-analysis-tool:${version}
7+
ECS_IMAGE := docker.io/library/dockerize-php-code-analysis-php-code-analysis-tool
8+
9+
# Define default path if one is not passed
10+
ifndef path
11+
ifneq ("$(wildcard src)","")
12+
path = src
13+
else
14+
path = app config database tests
15+
endif
16+
endif
17+
18+
ifneq ("$(wildcard ecs.php)","")
19+
ECS_DR := docker run -it --rm -v "$$PWD":/codebase -v "$$PWD/ecs.php":/app/ecs.php ${ECS_IMAGE} bash
20+
else
21+
ECS_DR := docker run -it --rm -v "$$PWD":/codebase ${ECS_IMAGE} bash
22+
endif
23+
24+
GIT_DIR = $(shell git rev-parse --show-toplevel)
25+
GIT_DIFF_DIRTY = $(shell git diff --name-only | grep '.php$$')
26+
GIT_DIFF_RANGE=origin/${branch}..HEAD
27+
GIT_DIFF_BRANCH=$(shell git diff --name-only --diff-filter=ACMRTUXB "${GIT_DIFF_RANGE}" | grep '.php$$')
28+
29+
##
30+
# @command code.check Check code in specified path. Usage: `make code.check path=src/app\ src/tests` (src by default)
31+
##
32+
code.check:
33+
${ECS_DR} \
34+
-c "ecs check --config /app/ecs.php ${path}"
35+
36+
##
37+
# @command code.fix Fix code in specified path. Usage: `make code.fix path=src/app\ src/tests` (src by default)
38+
##
39+
code.fix:
40+
${ECS_DR} \
41+
-c "ecs check --fix --config /app/ecs.php ${path}"
42+
43+
##
44+
# @command code.check.dirty Check only new code.
45+
##
46+
code.check.dirty:
47+
@cd ${GIT_DIR} && ${ECS_DR} \
48+
-c "ecs check --config /app/ecs.php ${GIT_DIFF_DIRTY}"
49+
50+
##
51+
# @command code.fix.dirty Fix only new code.
52+
##
53+
code.fix.dirty:
54+
@cd ${GIT_DIR} && ${ECS_DR} \
55+
-c "ecs check --fix --config /app/ecs.php ${GIT_DIFF_DIRTY}"
56+
57+
##
58+
# @command code.check.diff Check only diff files from target branch. Usage: `make code.check.diff branch=main` (develop by default)
59+
##
60+
code.check.diff:
61+
cd ${GIT_DIR} && ${ECS_DR} \
62+
-c "ecs check --config /app/ecs.php ${GIT_DIFF_BRANCH}"
63+
64+
##
65+
# @command code.fix.diff Fix only diff files from target branch. Usage: `make code.fix.diff branch=main` (develop by default)
66+
##
67+
code.fix.diff:
68+
cd ${GIT_DIR} && ${ECS_DR} \
69+
-c "ecs check --fix --config /app/ecs.php ${GIT_DIFF_BRANCH}"
70+
71+
##
72+
# @command code.config.publish Publish ecs.php configuration file to override default one.
73+
##
74+
code.config.publish:
75+
@echo '...........Publishing config'
76+
@$(eval CONTAINER_ID = $(shell docker create hub.jcdev.net:24000/php-code-analysis-tool:latest))
77+
@docker cp ${CONTAINER_ID}:/app/ecs.php ecs.php
78+
@docker rm -v ${CONTAINER_ID}
79+
@echo '...........Published "ecs.php"'

bin/colors-xterm.mk

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# define standard colors
2+
ifneq (,$(findstring xterm,${TERM}))
3+
FG_BLACK := $(shell tput -Txterm setaf 0)
4+
FG_RED := $(shell tput -Txterm setaf 1)
5+
FG_GREEN := $(shell tput -Txterm setaf 2)
6+
FG_YELLOW := $(shell tput -Txterm setaf 3)
7+
FG_LPURPLE := $(shell tput -Txterm setaf 4)
8+
FG_PURPLE := $(shell tput -Txterm setaf 5)
9+
FG_BLUE := $(shell tput -Txterm setaf 6)
10+
FG_WHITE := $(shell tput -Txterm setaf 7)
11+
FG_RESET := $(shell tput -Txterm sgr0)
12+
else
13+
FG_BLACK := ""
14+
FG_RED := ""
15+
FG_GREEN := ""
16+
FG_YELLOW := ""
17+
FG_LPURPLE := ""
18+
FG_PURPLE := ""
19+
FG_BLUE := ""
20+
FG_WHITE := ""
21+
FG_RESET := ""
22+
endif

bin/utils.mk

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.PHONY: help
2+
3+
UID := $(shell id -u)
4+
GID := $(shell id -g)
5+
6+
##
7+
# @command help Print commands list and command details
8+
#
9+
# @usage make help
10+
# @usage make help T=<target> Example: make help T=init
11+
##
12+
help:
13+
@echo "${PROJECT_NAME}"
14+
@if [ -z "${T}" ]; then \
15+
echo ''; \
16+
echo '${FG_YELLOW}Usage:${FG_RESET}'; \
17+
echo ' make'; \
18+
echo ' make <target> [options]'; \
19+
echo ''; \
20+
echo '${FG_YELLOW}Commands:${FG_RESET}'; \
21+
cat Makefile bin/*.mk | grep "#\s@command" | ${SED} 's/^# //g' | ${SED} 's/^@command/ /g'; \
22+
echo ''; \
23+
echo '${FG_PURPLE} * to learn more about command run: make help T=target${FG_RESET}'; \
24+
else \
25+
echo ''; \
26+
echo '${FG_YELLOW}Command help: ${FG_GREEN}${T}${FG_RESET}'; \
27+
cat Makefile bin/*.mk | grep -Pzo "(?s)##\n#\s@command\s${T}\s.*?##\n" | ${SED} 's/^#*/ /g'; \
28+
echo ''; \
29+
fi;
30+
31+
##
32+
# @command dotenv-set-uid Set current UID and GID to .env to run docker as non-root
33+
##
34+
dotenv-set-uid:
35+
${MAYBE_SUDO} ${SED} -i "s/USER_UID=9000.*/USER_UID=${UID}/g" .env;
36+
${MAYBE_SUDO} ${SED} -i "s/USER_GID=9000.*/USER_GID=${GID}/g" .env;

bin/vars.mk

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
USERID := $(shell id -u $$USER)
2+
CURDIR_BASENAME := $(notdir ${CURDIR})
3+
MAYBE_SUDO ?=
4+
VAGRANT_MODE ?=
5+
6+
SED := sed
7+
ifneq ($(shell command -v gsed), )
8+
SED := gsed
9+
endif
10+
11+
# parse .env vars
12+
ifneq ("$(wildcard .env)","")
13+
DOTENV := $(shell cat .env )
14+
# can be overridden from cli
15+
DB_DATABASE := $(shell cat .env | grep "^DB_DATABASE=" | ${SED} 's/DB_DATABASE=//')
16+
DB_USERNAME := $(shell cat .env | grep "^DB_USERNAME=" | ${SED} 's/DB_USERNAME=//')
17+
DB_PASSWORD := $(shell cat .env | grep "^DB_PASSWORD=" | ${SED} 's/DB_PASSWORD=//')
18+
endif
19+
20+
# define docker compose version
21+
DC := docker-compose
22+
DC2_VERSION := $(shell docker compose version | grep "version v2")
23+
ifneq "${DC2_VERSION}" ""
24+
DC := docker compose
25+
endif
26+
27+
DOCKER_COMPOSE_OVERRIDE_SRC := build/docker-compose.dev.yml
28+
29+
# check if we running a vagrant by default
30+
ifeq "$(USER)" "vagrant"
31+
V := 1
32+
endif
33+
# adjust global vars for vagrant mode
34+
ifneq "$(V)" ""
35+
VAGRANT_MODE := 1
36+
MAYBE_SUDO := sudo
37+
endif
38+
39+
# define do we need to disable docker pseudo TTY interface
40+
DOCKER_T_FLAG :=
41+
ifeq "$(NON_INTERACTIVE)" "1"
42+
DOCKER_T_FLAG := -T
43+
endif
44+
45+
# docker exec/run aliases
46+
DC_WORKDIR_WWW := -w /var/www/html
47+
DC_EXEC := ${DC} exec ${DOCKER_T_FLAG} --privileged --index=1
48+
DC_RUN := ${DC} run --rm ${DOCKER_T_FLAG}

0 commit comments

Comments
 (0)