Skip to content

BruceGitHub/namespace-protector

Repository files navigation

namespace-protector

A tool to validate namespace

Latest Stable Version Latest Unstable Version License Build Status Scrutinizer Code Quality codecov

Ispired by

and for fun ...

Motivation

Allow to improve the information hiding at level of namespace Like C#/Java pubblic/private class. The idea is that namespace in some situations can be private at all except for a specific entry point. For example the namespace of third parts lib.

The Design of this project follows this rules

  • No NULL
  • No instanceof
  • No switch
  • No static

Nice to have

  • Minimize the @var annotation

todo

10/10/2021

  • [] Review the db namespace
  • [] Remove psalm-suppress
  • [] Remove psalm minor issues

Waiting

  • [] Adds mode DISCOVER_CONFIG, so for each lib that in the extra node of composer.json declares the visibility settings of the namespace lib, it can perform the validity in autonomy

Done

  • Adds command to build conflicts graph

Minimal config

Trought the json configuration it's possible define

{
  "version": "0.1.0",
  "start-path": "src",
  "composer-json-path": "./",
  "public-entries": [],
  "private-entries": [],
  "mode": "MODE_MAKE_VENDOR_PRIVATE"
}

Fast because each ast it's cached and reused until the target file change

  • mode public default mode, in this setup only a private namespace it's validated
  • mode private vendor in which each access of vendor namespace trigger a violation if was not added public namespace.

Install and Run

with Composer

composer require --dev brucegithub/namespace-protector asciicast

with Phar

Adds this to composer.json

"repositories": [{
  "type": "vcs",
  "url": "https://github.com/brucegithub/namespace-protector-phar.git"
}],

composer require --dev brucegithub/namespace-protector-phar --no-cache "dev-main" asciicast

with Docker

docker run  --rm -it -v $(pwd):/namespace-protector brucedockerhub/namespace-protector:0.1.0 create-config
docker run  --rm -it -v $(pwd):/namespace-protector brucedockerhub/namespace-protector:0.1.0 validate-namespace

setup

vendor/bin/namespace-protector create-config

run

vendor/bin/namespace-protector validate-namespace

➜  namespace-protector git:(master) ✗ vendor/bin/namespace-protector validate-namespace
|Dump config:
|> Version: 0.1.0
|> Cache: FALSE
|> Plotter: plotter-terminal
|> Path start: tests/Stub/RealProject/src
|> Composer Json path: ./
|> Mode: PUBLIC
|> Private entries:
|       >NamespaceProtector\Common\
|       >NamespaceProtector\Scanner\
|       >PhpParser

|
|> Public entries:


Load data...
Loaded 3 files to validate
Loaded 5031 built in symbols
Start analysis...

Processed file: tests/Stub/RealProject/src/NamespaceProtectorProcessorFactory.php
	 > ERROR Line: 7 of use PhpParser\NodeTraverser
	 > ERROR Line: 8 of use PhpParser\ParserFactory
	 > ERROR Line: 14 of use NamespaceProtector\Scanner\ComposerJson
	 > ERROR Line: 16 of use NamespaceProtector\Common\FileSystemPath
	 > ERROR Line: 19 of use NamespaceProtector\Scanner\FileSystemScanner

Processed file: tests/Stub/RealProject/src/Analyser.php
	 > ERROR Line: 8 of use NamespaceProtector\Common\PathInterface

Processed file: tests/Stub/RealProject/src/EnvironmentDataLoader.php
	 > ERROR Line: 8 of use NamespaceProtector\Scanner\ComposerJson
Total files: 3
Total errors: 7
Elapsed time: 0.68148
Example of output in png format.
➜  vendord/bin/namespace-protector validate-namespace plotter-png

example output

For now it is a lab but...