Skip to content

Commit

Permalink
Rewrite FromDirectory source to use lang.reflection.Type::instantiable()
Browse files Browse the repository at this point in the history
...instead of checking for interfaces, traits and abstract classes
  • Loading branch information
thekid committed Mar 28, 2024
1 parent 097d4c0 commit 46fc70e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

## ?.?.? / ????-??-??

## 2.1.0 / 2024-03-28

* Made `FromDirectory` consistent with other source implementations in
regard to how it filters the types returned
(@thekid)

## 2.0.0 / 2024-03-23

This second major release upgrades this library to support only the latest
Expand Down
9 changes: 5 additions & 4 deletions src/main/php/test/source/FromDirectory.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace test\source;

use io\{Folder, Path};
use lang\{ClassLoader, FileSystemClassLoader, IllegalArgumentException};
use lang\{ClassLoader, FileSystemClassLoader, IllegalArgumentException, Reflection};
use test\execution\TestClass;

class FromDirectory extends Source {
Expand Down Expand Up @@ -42,7 +42,7 @@ private function testClassesIn($cl, $folder) {
} else if (($p= strpos($entry->name(), '.')) && 0 === substr_compare($entry->name(), 'Test', $p - 4, 4)) {
$uri= $entry->asURI();
if ($loader= $cl->findUri($uri)) {
yield $loader->loadUri($uri);
yield Reflection::type($loader->loadUri($uri));
}
}
}
Expand All @@ -51,8 +51,9 @@ private function testClassesIn($cl, $folder) {
/** @return iterable */
public function groups() {
foreach ($this->testClassesIn(ClassLoader::getDefault(), $this->folder) as $class) {
if ($class->isInterface() || $class->isTrait() || $class->getModifiers() & MODIFIER_ABSTRACT) continue;
yield new TestClass($class);
if ($class->instantiable()) {
yield new TestClass($class);
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/php/test/source/FromFile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public function __construct($arg) {
}

if ($loader= ClassLoader::getDefault()->findUri($this->path)) {
$this->type= $loader->loadUri($this->path);
$this->type= Reflection::type($loader->loadUri($this->path));
return;
}

throw new IllegalArgumentException($this->path.' is not in class path');
}

/** Returns the type discovered from the file */
public function type(): Type { return Reflection::type($this->type); }
public function type(): Type { return $this->type; }

/** @return iterable */
public function groups() {
yield new TestClass($this->type());
yield new TestClass($this->type);
}

/** @return string */
Expand Down
4 changes: 3 additions & 1 deletion src/main/php/test/source/FromPackage.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ private function typesIn($package) {
/** @return iterable */
public function groups() {
foreach ($this->typesIn($this->package) as $type) {
if ($type->instantiable() && 0 === substr_compare($type->name(), 'Test', -4, 4)) yield new TestClass($type);
if ($type->instantiable() && 0 === substr_compare($type->name(), 'Test', -4, 4)) {
yield new TestClass($type);
}
}
}

Expand Down

0 comments on commit 46fc70e

Please sign in to comment.