OSDN Git Service

add checking method visibility
authorfrostbane <frostbane@programmer.net>
Thu, 28 Nov 2019 06:07:19 +0000 (15:07 +0900)
committerfrostbane <frostbane@programmer.net>
Thu, 28 Nov 2019 06:39:30 +0000 (15:39 +0900)
src/Route.php
tests/Fixtures/CustomController.php
tests/Fixtures/SomeController.php
tests/src/PHPRouterTest/RouteTest.php

index 30237f0..528fbce 100755 (executable)
@@ -57,12 +57,12 @@ class Route
     private $class;
 
     /**
-     * @param       $resource
-     * @param array $config
+     * @param string $pathInfo
+     * @param array  $config
      */
-    public function __construct($resource, array $config)
+    public function __construct($pathInfo, array $config)
     {
-        $this->url        = $resource;
+        $this->url        = $pathInfo;
         $this->config     = $config;
         $this->methods    = isset($config['methods']) ? (array)$config['methods'] : array();
         $this->target     = isset($config['target']) ? $config['target'] : null;
@@ -177,11 +177,17 @@ class Route
         }
 
         try {
-            $classRexl->getMethod($method);
+            $methRexl = $classRexl->getMethod($method);
         } catch (\ReflectionException $ex) {
             return null;
         }
 
+        // use only public methods
+        // to avoid calling inherited protected methods
+        if(!$methRexl->isPublic()) {
+            return null;
+        }
+
         return $this->config['_controller'];
     }
 
index e768bdb..6d597e5 100644 (file)
@@ -4,6 +4,9 @@ namespace PHPRouter\Test\Fixtures;
 
 final class CustomController
 {
+    /**
+     * @var string
+     */
     private $config;
 
     public function __construct($config)
@@ -13,5 +16,11 @@ final class CustomController
 
     public function index()
     {
+        return "index $this->config";
+    }
+
+    public function home()
+    {
+        echo "home $this->config";
     }
 }
index 3a785c1..9605fe3 100644 (file)
@@ -4,12 +4,18 @@ namespace PHPRouter\Test\Fixtures;
 
 final class SomeController
 {
+    private function privateMethod()
+    {
+    }
+
     public function usersCreate()
     {
+        echo "register user";
     }
 
     public function indexAction()
     {
+        echo "index";
     }
 
     public function user()
index 23cc725..107bb0e 100644 (file)
@@ -12,6 +12,7 @@ class RouteTest extends PHPUnit_Framework_TestCase
     private $routeUsing__invoke;
     private $routeWithParameters;
     private $routeInvalid;
+    private $routeWithPrivateMethod;
 
     protected function setUp()
     {
@@ -29,6 +30,16 @@ class RouteTest extends PHPUnit_Framework_TestCase
             )
         );
 
+        $this->routeWithPrivateMethod = new Route(
+            '/home/:user/:id',
+            array(
+                '_controller' => '\PHPRouter\Test\Fixtures\SomeController::privateMethod',
+                'methods'     => array(
+                    'GET',
+                ),
+            )
+        );
+
         $this->routeWithParameters = new Route(
             '/page/:page_id/:page_size',
             array(
@@ -123,6 +134,11 @@ class RouteTest extends PHPUnit_Framework_TestCase
         $this->assertNull($this->routeInvalid->getValidController());
     }
 
+    public function testGetValidController_privateMethod()
+    {
+        $this->assertNull($this->routeWithPrivateMethod->getValidController());
+    }
+
     public function testSetGetParameters()
     {
         $param = array("page_id" => 123);
@@ -220,6 +236,8 @@ class RouteTest extends PHPUnit_Framework_TestCase
         $rexl->setAccessible(true);
 
         $provider = array(
+            array(true, true, "1"),
+            array(true, false, ""),
             array(true, 1, "1"),
             array(true, 0, "0"),
             array(true, 1.23, "1.23"),
@@ -228,6 +246,7 @@ class RouteTest extends PHPUnit_Framework_TestCase
             array(true, new PrintableDate(), "to string"),
             array(false, null),
             array(false, array()),
+            array(false, date_create()),
         );
 
         foreach ($provider as $case) {