Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
sdrobov committed Dec 12, 2018
2 parents 7155473 + e1fbb41 commit 3ab7ccb
Show file tree
Hide file tree
Showing 28 changed files with 2,642 additions and 190 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/vendor/
.idea
/phar/
autopsr4.phar
22 changes: 22 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
language: php
php:
- '7.2'

before_script:
- composer install

jobs:
include:
- stage: test
script: phpunit
- stage: build
script: php create-phar.php

deploy:
provider: releases
api_key:
secure: pO7Gxu/icU/KdyrB2kP53qWEk7/n3d//MmWDvveZliVxM5c82JRY2rRsjWlx+a6BPDCs6LBvX3wMuTZrXxCCIJLLrckfUrQhyx4r/qOuXA4dv59GZdXsEFnsuVt1AHoL7vRYa36t65AHaEuy2wSLS+A/iFwECTSQ8F1IQ3ZGwQQGR2xkMk5xzRcZIhJ/CJx37mYgEvQ16FI+lcKxpMGf2aHO1g403QAgA9azUjwc0zLJzt11oK/imD/xKdSMh7IBKnr8R2UdV/B5HCkr0IsV8ESrXJwL4FdTtKOt9ouEam9AY27Eq3+ahpQIjbo0pej7tbm9Hfvk/M/Jz1wbcWOcbjtmslYRWdfbD1OVtKjgcfJqOhjCcshmQp6QAR5wRfS8633Oq9lvt/EExEM5+0YZsaKgFPcBO3HYO79HI8FvWVDRVXSqGxQkidNkqY0RTNECqCBrmzBZptA6YwPldGq4eNfa1u9Gd2obIjQj1rNC/6hGhfr74EAvUPmKOclWAIyUKByM1LTMvemTGXjWteWSlQ4s+i7TDzNh5fiMC+AHfRw4UTYtAGShq6Rfpx7MywwtjsrXQOG+k06mmSup8AZM9/cLuCUSBLY/e2cnB6avInRyFb20RJsPnQY74kk5JlfpDmvVadeFp0dY61L+lCZ7Sie0Sb60n0lQPZNnl9e/dcY=
file: autopsr4.phar
skip_cleanup: true
on:
tags: true
20 changes: 20 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2018 Sergey Drobov <serch.m@gmail.com>.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## About
[![Build Status](https://travis-ci.org/sdrobov/autopsr4.svg?branch=master)](https://travis-ci.org/sdrobov/autopsr4)

AutoPsr4 is a small tool intended to help to convert legacy non PSR4 projects to PSR4.
Project must have a compatible structure: all files should follow one-class-per-file rule,
if file is under subdirectory `Subdir` and called `Filename.php` it must have classname
`Subdir_Filename`. Pretty strong rules and I don't know if this utility be useful for someone
except me, but if it is I will be glad.
191 changes: 2 additions & 189 deletions autopsr4.php
Original file line number Diff line number Diff line change
@@ -1,194 +1,7 @@
#!/usr/bin/php
<?php

if ($argc != 3) {
usage();
}
require_once __DIR__ . '/vendor/autoload.php';

$srcRoot = realpath($argv[1]);
$rootNs = $argv[2];
(new \AutoPsr4\App())->run($argv);

function usage()
{
global $argv;

echo "{$argv[0]} <src root> <root namespace>";

die;
}

function dirRecursive($dirname)
{
global $srcRoot;

$files = [];
foreach (glob("{$dirname}/*") as $entity) {
if (preg_match('/\.php$/', $entity)) {
$entity = str_replace(dirname($srcRoot) . '/', '', $entity);
$files[] = $entity;

continue;
}

if (is_dir($entity)) {
$dirContent = dirRecursive("$entity");
$files = array_merge($files, $dirContent);
}
}

return $files;
}

$files = dirRecursive($srcRoot);
$fileReplaceMap = [];
$classReplaceMap = [];

echo 'Step1: building replace maps' . PHP_EOL;
foreach ($files as $file) {
echo "Processing {$file}...";

$parts = explode('/', $file);
array_shift($parts);

$possibleClassName = array_pop($parts);
$possibleClassName = str_replace('.php', '', $possibleClassName);

$ns = $rootNs . (empty($parts) ? '' : '\\' . implode("\\", $parts));

$content = file_get_contents($file);

$match = [];
if (preg_match('/^namespace ([^;]+);$/m', $content, $match)) {
if ($match[1] == $ns) {
echo ' file already has valid namespace, SKIP' . PHP_EOL;

continue;
}

echo " file has namespace {$match[1]}, but it doesnt match {$ns} we guess for it, SKIP" . PHP_EOL;

continue;
}

$match = [];
if (!preg_match('/^(?:abstract )?(?:class|interface) (\w+)/m', $content, $match)) {
echo ' class not found, SKIP' . PHP_EOL;

continue;
}

$oldClassName = $match[1];
$classParts = explode('_', $oldClassName);
$realClassName = array_pop($classParts);

if ($realClassName != $possibleClassName) {
echo " {$realClassName} != {$possibleClassName}, SKIP" . PHP_EOL;

continue;
}

$fqn = "{$ns}\\{$realClassName}";
echo "; old class name: {$oldClassName}; new FQN: {$fqn};";
$fileReplaceMap[$file] = [
'className' => $realClassName,
'ns' => $ns,
'fqn' => $fqn,
];
$classReplaceMap[$oldClassName] = $fqn;

echo ' DONE' . PHP_EOL;
}

echo PHP_EOL . 'Step 2: finding usages' . PHP_EOL;
foreach ($fileReplaceMap as $file => &$replaces) {
echo "Processing {$file}... ";

$content = file_get_contents($file);

$replaces['usages'] = [];
$replaces['uses'] = [];
foreach ($classReplaceMap as $old => $new) {
$parts = explode('\\', $new);
$shortName = array_pop($parts);
$ns = implode('\\', $parts);

if ($new == $replaces['fqn'] || $ns == $replaces['ns']) {
$replaces['usages'][$old] = $shortName;

continue;
}

$match = [];
if (
preg_match_all(
"/(?<!class )(?<!interface )(?<!namespace )(?<!\$)(?<!\w){$old}(?!\w)/",
$content,
$match
)
) {
if (in_array($shortName, $replaces['usages']) && !array_key_exists($old, $replaces['usages'])) {
if (count($parts) > 1) {
$shortName = end($parts) . $shortName;
} else {
$shortName .= '1';
}

$new .= " as {$shortName}";
}

$replaces['uses'][] = $new;
$replaces['usages'][$old] = $shortName;
}
}

echo 'DONE' . PHP_EOL;
}

echo PHP_EOL . 'Step 3: doing replaces' . PHP_EOL;
foreach ($fileReplaceMap as $file => $replaces) {
echo "Processing {$file}... ";

$content = file_get_contents($file);

$content = preg_replace(
'/\<\?(php)?\n(\n?\/\*(?:[^\/]|\n)+\*\/\n)?/ms',
"<?php\n$2\n\nnamespace {$replaces['ns']};\n\n",
$content
);
$content = preg_replace(
'/^(abstract )?(class|interface) \w+/m',
"$1$2 {$replaces['className']}",
$content,
1
);

if (!empty($replaces['uses'])) {
$uses = implode(
"\n",
array_map(
function ($use) {
return "use {$use};";
},
$replaces['uses']
)
);

$content = preg_replace(
'/^^(\/\*(?:[^\/]|\n)+\*\/\n)?(abstract )?(class|interface)/m',
"{$uses}\n\n$1$2$3",
$content
);
}

foreach ($replaces['usages'] as $old => $new) {
$content = preg_replace(
"/(?<!class )(?<!interface )(?<!namespace )(?<!\$)(?<!\\\\)(?<!\w)(?<!')(?<!\"){$old}(?!\w)/",
$new,
$content
);
}

file_put_contents($file, $content);

echo 'DONE' . PHP_EOL;
}
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@
"email": "serch.m@gmail.com"
}
],
"require": {}
"autoload": {
"psr-4": {
"AutoPsr4\\": "src"
}
},
"require": {},
"require-dev": {
"phpunit/phpunit": "^7"
}
}
Loading

0 comments on commit 3ab7ccb

Please sign in to comment.