-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add autodiscovery of --init folders based on composer.json
- Loading branch information
Showing
11 changed files
with
246 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,125 @@ | |||
<?php | |||
namespace Psalm\Config; | |||
|
|||
use Psalm\Exception\ConfigCreationException; | |||
use Psalm\Internal\Provider; | |||
|
|||
class Creator | |||
{ | |||
public static function getContents( | |||
string $current_dir, | |||
string $suggested_dir = null, | |||
int $level = 3 | |||
) : string { | |||
$replacements = []; | |||
|
|||
if ($suggested_dir) { | |||
if (is_dir($current_dir . DIRECTORY_SEPARATOR . $suggested_dir)) { | |||
$replacements[] = '<directory name="' . $suggested_dir . '" />'; | |||
} else { | |||
$bad_dir_path = $current_dir . DIRECTORY_SEPARATOR . $suggested_dir; | |||
|
|||
throw new ConfigCreationException( | |||
'The given path "' . $bad_dir_path . '" does not appear to be a directory' | |||
); | |||
} | |||
} elseif (is_dir($current_dir . DIRECTORY_SEPARATOR . 'src')) { | |||
$replacements[] = '<directory name="src" />'; | |||
} else { | |||
$composer_json_location = $current_dir . DIRECTORY_SEPARATOR . 'composer.json'; | |||
|
|||
if (!file_exists($composer_json_location)) { | |||
throw new ConfigCreationException( | |||
'Problem during config autodiscovery - could not find composer.json during initialization.' | |||
); | |||
} | |||
|
|||
/** @psalm-suppress MixedAssignment */ | |||
if (!$composer_json = json_decode(file_get_contents($composer_json_location), true)) { | |||
throw new ConfigCreationException('Invalid composer.json at ' . $composer_json_location); | |||
} | |||
|
|||
if (!is_array($composer_json)) { | |||
throw new ConfigCreationException('Invalid composer.json at ' . $composer_json_location); | |||
} | |||
|
|||
$replacements = self::getPsr4Paths($current_dir, $composer_json); | |||
|
|||
if (!$replacements) { | |||
throw new ConfigCreationException( | |||
'Could not located any PSR-4-compatible paths in ' . $composer_json_location | |||
); | |||
} | |||
} | |||
|
|||
$template_file_name = dirname(__DIR__, 3) . '/assets/config_levels/' . $level . '.xml'; | |||
|
|||
if (!file_exists($template_file_name)) { | |||
throw new ConfigCreationException('Could not open config template ' . $template_file_name); | |||
} | |||
|
|||
$template = (string)file_get_contents($template_file_name); | |||
|
|||
$template = str_replace( | |||
'<directory name="src" />', | |||
implode("\n ", $replacements), | |||
$template | |||
); | |||
|
|||
return $template; | |||
} | |||
|
|||
/** | |||
* @return string[] | |||
* @psalm-suppress MixedAssignment | |||
* @psalm-suppress MixedOperand | |||
*/ | |||
private static function getPsr4Paths(string $current_dir, array $composer_json) : array | |||
{ | |||
if (!isset($composer_json['autoload']['psr-4'])) { | |||
return []; | |||
} | |||
|
|||
$nodes = []; | |||
|
|||
/** @var string|string[] $path */ | |||
foreach ($composer_json['autoload']['psr-4'] as $paths) { | |||
if (!is_array($paths)) { | |||
$paths = [$paths]; | |||
} | |||
|
|||
foreach ($paths as $path) { | |||
if ($path === '') { | |||
/** @var string[] */ | |||
$php_files = array_merge( | |||
glob($current_dir . DIRECTORY_SEPARATOR . '*.php'), | |||
glob($current_dir . DIRECTORY_SEPARATOR . '**/*.php'), | |||
glob($current_dir . DIRECTORY_SEPARATOR . '**/**/*.php') | |||
); | |||
|
|||
foreach ($php_files as $php_file) { | |||
$parts = explode(DIRECTORY_SEPARATOR, $php_file); | |||
|
|||
if ($parts[0] === 'vendor') { | |||
continue; | |||
} | |||
|
|||
if (count($parts) === 1) { | |||
$nodes[] = '<file name="' . $php_file . '" />'; | |||
} else { | |||
$nodes[] = '<file name="' . $parts[0] . '" />'; | |||
} | |||
} | |||
} else { | |||
$nodes[] = '<directory name="' . $path . '" />'; | |||
} | |||
} | |||
} | |||
|
|||
$nodes = array_unique($nodes); | |||
|
|||
sort($nodes); | |||
|
|||
return $nodes; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,6 @@ | |||
<?php | |||
namespace Psalm\Exception; | |||
|
|||
class ConfigCreationException extends \Exception | |||
{ | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,56 @@ | |||
<?php | |||
namespace Psalm\Tests\Config; | |||
|
|||
use Psalm\Config\Creator; | |||
|
|||
class CreatorTest extends \Psalm\Tests\TestCase | |||
{ | |||
/** | |||
* @return void | |||
*/ | |||
public static function setUpBeforeClass() | |||
{ | |||
} | |||
|
|||
/** | |||
* @return void | |||
*/ | |||
public function setUp() | |||
{ | |||
} | |||
|
|||
/** | |||
* @return void | |||
*/ | |||
public function testDiscoverLibDirectory() | |||
{ | |||
$lib_contents = Creator::getContents( | |||
dirname(__DIR__, 1) | |||
. DIRECTORY_SEPARATOR . 'fixtures' | |||
. DIRECTORY_SEPARATOR . 'config_discovery' | |||
. DIRECTORY_SEPARATOR . 'files_in_lib', | |||
null, | |||
1 | |||
); | |||
|
|||
$this->assertSame('<?xml version="1.0"?> | |||
<psalm | |||
totallyTyped="true" | |||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
xmlns="https://getpsalm.org/schema/config" | |||
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" | |||
> | |||
<projectFiles> | |||
<directory name="lib" /> | |||
<ignoreFiles> | |||
<directory name="vendor" /> | |||
</ignoreFiles> | |||
</projectFiles> | |||
<issueHandlers> | |||
<LessSpecificReturnType errorLevel="info" /> | |||
</issueHandlers> | |||
</psalm> | |||
', $lib_contents); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
{ | |||
"name": "dummy/lib", | |||
"autoload": { | |||
"psr-4": { | |||
"Foo\\Bar": "lib" | |||
} | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
<?php | |||
|
|||
namespace Foo\Bar; | |||
|
|||
class Baz {} |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/config_discovery/files_in_lib/vendor/somefile.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
<?php | |||
|
|||
// some file |
Oops, something went wrong.